• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either syntax works. The first is much more common and reads better for most folks.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) {}
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic