• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is "String..." ?  RSS feed

 
Jackson Martin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is difference between
public static void main(String args[])
and
public static void main(String... args)

Please give more detail on "String..."
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ellipsis operator … takes all the arguments which follow it (they must be all the same type) and converts them to an array. So there will be no discernible difference between the two forms of declaration in the main method.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java® Tutorials page. Scroll about 40% of the way down.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This feature is called "varargs" (variable number of arguments). An explanation: Varargs
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leo Messi wrote:What is difference between



Something still doesn't add up. From appearances...the first one puts the args into an array, and the second one accepts a variable number of Strings, but there is no array of strings.
Are they treated the same way by main() and etc. anyway so the difference is irrelevant?
 
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
Guillermo Ishi wrote:
Something still doesn't add up. From appearances...the first one puts the args into an array, and the second one accepts a variable number of Strings, but there is no array of strings.
Are they treated the same way by main() and etc. anyway so the difference is irrelevant?


From the called method side, I don't think that there is a bit of difference. In both cases, the method is expecting an array to be passed as the last parameter.

From the compiler point of view, I believe that something is marked in the byte code. And I know that those in the know, will quickly correct me if I am wrong here... ... if a method is declared as a vararg, it is marked as such. This way, the compiler has the option of generating special code that creates the array using the trailing parameters as the array elements. If it was declared as an array, I don't think the compiler will process it as a vararg from the calling side.

Henr
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL I just realized args... is accessed as an array; args[0], args[1], and so on. I knew the rules for using varargs from the test stuff but never had to actually access them...
 
Jackson Martin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Java® Tutorials page. Scroll about 40% of the way down.


got it.
Thanks.
Link provided by you is great.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!