aspose file tools*
The moose likes Beginning Java and the fly likes signature of main() method.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "signature of main() method...." Watch "signature of main() method...." New topic
Author

signature of main() method....

Naveen Megharaj
Ranch Hand

Joined: Feb 13, 2009
Posts: 39
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Naveen Megharaj
Ranch Hand

Joined: Feb 13, 2009
Posts: 39
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

Joined: Sep 29, 2008
Posts: 12617

Answer it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Varun K. Gupta
Greenhorn

Joined: Jun 14, 2009
Posts: 10
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

Joined: Feb 13, 2009
Posts: 39
thanks a lot to all the repliers..........
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10372
    
    8

"Fundu Varun"

Welcome to the Ranch.
Please check your private messages for an important administrative matter.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: signature of main() method....