Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "public Weatherman(Integer... zips) {" mean in java

I am trying to read some Java code from a tutorial, I don't understand the line:

 public Weatherman(Integer... zips) {
  • I don't understand what the ... represents if it was just (Integer zips) I would understand that there is a variable of class Integer called zips. But the ... are confusing me.
like image 652
Ankur Avatar asked Jul 22 '09 14:07

Ankur


2 Answers

Those are "varargs," syntactic sugar that allows you to invoke the constructor in the following ways:

new Weatherman()
new Weatherman(98115);
new Weatherman(98115, 98072);
new Weatherman(new Integer[0]);

Under the covers the arguments are passed to the constructor as an array, but you do not need to construct an array to invoke it.

like image 160
Steve Reed Avatar answered Sep 19 '22 01:09

Steve Reed


That’s a “vararg”. It can handle any number of Integer arguments, i.e.

new Weatherman(1);

is just as valid as

new Weatherman();

or

new Weatherman(1, 7, 12);

Within the method you access the parameters as an Integer array.

like image 23
Bombe Avatar answered Sep 19 '22 01:09

Bombe