• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Barb Rudnick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either syntax works. The first is much more common and reads better for most folks.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that the name given to the String array can be any legal identifier. For example...

public static void main(String[] fred) {}
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Sam Francis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barb Rudnick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic