Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

signature of main() method....

 
Naveen Megharaj
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it mandatory that the signature of main() method should be as shown below in order to run the application:

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


OR is it ok even if the signature is like this:

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

And also i wanted to know the difference between the two method signatures........?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naveen,
String... didn't exist prior to Java 5 meaning older code must use the first form.

Since String... and String[] compile to the same thing, newer code can use either form. I tend to use String... because it's faster to type.
 
Naveen Megharaj
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which means at present both method signatures are valid and legal isn't it........? if this questions is asked in SCJP exam what should i do........?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer it.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:I tend to use String... because it's faster to type.

Really? ... is 3 characters while [] is 2 located directly next to each other on most keyboards. I also tend to use ... though.
 
Varun K. Gupta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as a method with single parameter is concerned not much difference is involved. However few good points can be consider in case of ELLIPSIS(...)

1) There can be ONLY ONE ellipsis parameter in the method and which MUST be the last parameter. There in no such restriction for String[] version
2) It can be analysed using int printf(String,...) method of C Programming in which it can accept variable number of arguments. like printf("%d",1);printf("%d%f",1,1.0);
3) Elipsis provide a way to define single method defination for variable argument calls with ZERO TO 'N' number of arguments of same or type that can be IMPLICITLY CASTED to that parameter.
4) Ellipsis version comes with 5.0 version.
5) Ellipsis provides more freedom as compare to array[] version.

No hard rules which to use when.
 
Naveen Megharaj
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot to all the repliers..........
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Fundu Varun"

Welcome to the Ranch.
Please check your private messages for an important administrative matter.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Jeanne Boyarsky wrote:I tend to use String... because it's faster to type.

Really? ... is 3 characters while [] is 2 located directly next to each other on most keyboards. I also tend to use ... though.

Yes. But it is faster to touch type the same character three times than two different characters. Also, the dot character is in a position easier to reach while touch typing than the [] because I have to move my finger a good distance to get to the "]". Which often requires me to look at the keyboard further reducing speed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic