Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's the difference between String[] and String... ?

 
Raphale Style
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Referring to the book OCA/COP Java SE 7 ProrogrammerI&II Study Guide by Kathy Sierra and Bert Bates.

In page 13 as quoted below:

"The followings are all legal declarations for the special main():
static public void main(String[] args)
public static void main(String... x)
static public void main(String bang_a_gong[])
"

I never saw String... before. My research showed they do the same job.

What's the difference between String[] and String...? When to use each?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
 
Raphale Style
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, I've found enough information in the book Think in Java 4th edition by Bruce Eckel, page 137 under the title "Variable Argument Lists".

As far as I can see, "String... x" is a not-that-old feature added by SE 5. It works smarter than "String[] x".

I will save the details to my future study of arrays.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raphale,

First of all, a warm welcome to CodeRanch!

Raphale Style wrote:Thank you guys, I've found enough information in the book Think in Java 4th edition by Bruce Eckel, page 137 under the title "Variable Argument Lists".

No need to panic! When you reach page 48 of K&B7 var-args will be discussed as well

Raphale Style wrote:As far as I can see, "String... x" is a not-that-old feature added by SE 5. It works smarter than "String[] x".

Yes, it is! It is mentioned in the book as well (even in the sentence just before your quote: ...and as of Java 5 it can be declared using var-args syntax.). But if you don't know String... is called var-args, then you can't link the statement with the appropriate line of code. Maybe the 2nd example of the main declaration should have a clarifying comment, like // var-args.

Kind regards,
Roel
 
Raphale Style
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your welcome Nijs. Indeed it's my first post in this forum!

Roel De Nijs wrote:Hi Raphale,

But if you don't know String... is called var-args, then you can't link the statement with the appropriate line of code. Maybe the 2nd example of the main declaration should have a clarifying comment, like // var-args.

Roel


Exactly how I feel. It seems no way to google "String...". I could continue the research only after I got the keyword variable arguments or var-args.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic