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
posted
0
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
posted
0
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,
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
posted
0
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
posted
0
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
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
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
posted
0
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
posted
0
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: 18670
posted
0
[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
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: 18670
posted
0
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 ]