wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between String[] and String...? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between String[] and String...?" Watch "Difference between String[] and String...?" New topic
Author

Difference between String[] and String...?

Hari Dhanakoti
Ranch Hand

Joined: Jan 07, 2008
Posts: 74
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...


Regards,
Hari
Kate Jackson
Greenhorn

Joined: Jan 12, 2010
Posts: 13
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

Joined: Jan 14, 2010
Posts: 317

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

Joined: Mar 20, 2010
Posts: 105
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...


SCJP 6
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

nice discussion
http://www.coderanch.com/t/385398/Java-General/java/String-array-Vs-String-var
Hari Dhanakoti
Ranch Hand

Joined: Jan 07, 2008
Posts: 74
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between String[] and String...?
 
Similar Threads
Anonymous Inner Class
primitve assignments
left shift operator
IF strings are immutable why is the variable s appending and changing its obect
Problem in javac and java command