aspose file tools*
The moose likes Beginning Java and the fly likes main(String args[]) versus main(String[] args) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "main(String args[]) versus main(String[] args)" Watch "main(String args[]) versus main(String[] args)" New topic
Author

main(String args[]) versus main(String[] args)

Barb Rudnick
Greenhorn

Joined: Feb 03, 2005
Posts: 20
Hey y'all!

Is there a difference between these two main method definitions?

public static void main ( String[] args )

public static void main ( String args[] )

In my short experience, I've always used the 1st method.

Best regards...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Either syntax works. The first is much more common and reads better for most folks.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Hi Barb,

There is no difference, both are legal declarations of the main() method.
In String args[], args[] is nothing but an array of type String and holds the arguments you pass to your program at runtime. An array can be legally declared as follows,


The above is an array of type integer.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Also note that the name given to the String array can be any legal identifier. For example...

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


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Also note that if you are using the current version of Java (1.5), you can write:
And as long as we are talking about mains, I wish I had a dime for every time
I saw a main where someone wrote

Instead of just:

(This is just is quick-and-dirty-code, of course. In real code, log the error.)


There is no emoticon for what I am feeling!
Sam Francis
Greenhorn

Joined: Oct 30, 2005
Posts: 7
Hi

in this context, i would like to ask abt varargs in functions as the 1.5 ver gave this option but it is only to the last arg. is it not convinent top have the option for allt he argumennts instead of the last? is there any other option for this?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Sam Francis:
is it not convinent top have the option for allt he argumennts instead of the last?


Explain to me how these would work:

int whatDoesThisDo(String... x, String... y) ...
int orThis(int... x, Integer... y) ...
int orEvenThis(double... x, Float... y) ...

i.e., what are the values of x and y if I say:

whatDoesThisDo("a", "b", "c");
orThis(1, 2, 3, 4, 5);
orEvenThis(1, 2, 3, 4, 5);

Yes, you could introduce a bunch of rules to make this work -- but in general, Java tries to avoid having a bunch of complicated rules.


[Jess in Action][AskingGoodQuestions]
Barb Rudnick
Greenhorn

Joined: Feb 03, 2005
Posts: 20
I just started a Java class and my instructor mentioned the second approach is preferred, BUT... I have to agree that using the first approach for defining an array (int[] = int) that one can very quickly see the variable is an array w/o having to read the entire line of code.

Thank you very much.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Barb Rudnick:
my instructor mentioned the second approach is preferred,.


It's just a matter of taste, but the second approach is the "old" style syntax inherited from C/C++, and the first approach (i.e., "int[] x") is the idiomatic Java way. I've never seen a Java programmer who preferred the second approach, so I think your instructor's tastes are somewhat out of the mainstream.
Sam Francis
Greenhorn

Joined: Oct 30, 2005
Posts: 7
Quote:
xplain to me how these would work:

int whatDoesThisDo(String... x, String... y) ...
int orThis(int... x, Integer... y) ...
int orEvenThis(double... x, Float... y) ...

i.e., what are the values of x and y if I say:

------------------------

I understand it, however, the datatypes can be different like: the second and third ones.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Sam Francis:
I understand it, however, the datatypes can be different like: the second and third ones.


Beware the autobox my son. The following is perfectly legal:

So allowing a method like void f(Integer...a, int...b) sounds problematic.
I think Java hit the sweet spot, a nice light snack of syntactic sugar.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Sam]: xplain to me how these would work:

They wouldn't. You can only have one ... in a method signature, and you may not have any other arguments after it.


"I'm not back." - Bill Harding, Twister
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Jim Yingst:
[b]
They wouldn't. You can only have one ... in a method signature, and you may not have any other arguments after it.


You missed the hypothetical, Jim -- he was asking why only the last argument was allowed to have an ellipsis.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ah, I misread the funky quoting style, and the response formatted like a signature for some reason. And I didn't read the preceding thread closely enough because I assumed that any questions prior to EFH's last post had already been answered completely.
[ November 03, 2005: Message edited by: Jim Yingst ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main(String args[]) versus main(String[] args)
 
Similar Threads
odd array signature
Subtle Difference...
Main class not recognized
JAVAC not found ??? I cant compile - help
none applicable in Eclipse IDE