Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"..." being used in Java? [duplicate]

Possible Duplicate:
Can I pass an array as arguments to a method with variable arguments in Java?
What is … in a method signature

I first saw this when I was modding Minecraft. It had a constructor that specified (String ... line), and thought it was just some shorthand that Mojang had created. But now, I was looking over ProcessBuider, and saw it again. I was wondering what this is used for. My best guess is that it allows developers to add as many of that type of object as they want. But if that's the case, why not just use an Array or List?

So, really, I am asking two questions:

  1. What is the "..." operator, and
  2. Why would it be more useful than using an Array or List?
like image 635
mattbdean Avatar asked Dec 06 '25 10:12

mattbdean


1 Answers

... indicates a multiple argument list to a variadic function: a function that can take a variable number of arguments.

For an example of this, look at PrintStream.format. The first (required) argument is a format String, and the remaining 0 or more arguments fulfill that format.

like image 126
pb2q Avatar answered Dec 07 '25 23:12

pb2q



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!