This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between String[] and String...?

 
Hari Dhanakoti
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

I have a simple doubt about whats the difference in specifying the String[] and String...

We make use of the String... in the code snippet as such

public static void main (String... s){}

public static void main (String[] s){}

Whats the technical / logical advantage of using as such instead...
 
Kate Jackson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hari Dhanakoti wrote:
We make use of the String... in the code snippet as such



As for main method I guess there's no difference. But in this case:

For 1 you can pass either varargs (surprise, surprise ) or a String array.
For 2 only String array will work.
The most strange part for me is that signature for both methods is equal: compiler will complain about duplicated method if you put both of them into one class.

If I'm wrong please correct me.
Kate

//edit: just figured it out
http://www.coderanch.com/t/385398/Java-General/java/String-array-Vs-String-var
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is one more difference that var arg method can be called without any argument.
So if you have a method like

it can be called as
 
ujjawal rohra
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler will complain because a var-args method
considers the argument passed to it as an array and the other method too takes an array
therefore its an ambiguity...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
 
Hari Dhanakoti
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,

The concept of having the varargs is achieved in a much better way by this specification..

I am now clear with the concept and thanks a lot...

Earlier, it was bit confusing and now clear....


Cheers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic