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.

Answer

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.

Leave a Reply

Your email address will not be published. Required fields are marked *