• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about ... use  RSS feed

 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been exposing myself to more things lately. I see a lot of methods that take parameters with ... in them. I know it's not morse code because I'm pretty sure that was depreciated with Java 1.3 (LOL!!)

What does it mean, how is it used, what is it called? (so maybe I can look up more stuff on google one day.... it's not easy to search for '...')
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your search keyword should be "varargs".

Short answer: it's a way to define a method that takes a variable number of arguments, all of the same type.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read a bit. It looks like it's sort-of used like an array? Are there benefits to this over using a real array as a parameter?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can just list the elements as parameters rather than collecting them into an array prior to making the call.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varargs in Java works like a kind of syntactic sugar for arrays. Note that you can change any method that takes an array and change it to a method that takes varargs; to the JVM, the methods will look the same. So you can even change your main() method to this:
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you override, like this?



If so, which one gets picked at runtime?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:Can you override, like this?

If so, which one gets picked at runtime?



Do you mean overload? Then, no. Since var-args with string is just an string array, the compiler will complain that the two methods have the same signature.

Henry
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice: the third version can be coexist with either of the other two, as an overload. But the first two cannot exist in the same class. If you call methodOne("A") (with exactly one argument) then the one-argument version (version 3) will be called in preference to either other version. For any other number of arguments, the one-argument version cannot be called.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!