I am trying to read some Java code from a tutorial, I don't understand the line:
public Weatherman(Integer... zips) {
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With