Reputation: 43
I am new in programming with Java and I am confused about the following two statements:
public static void main(String args[])
and
public static void main(String[] args)
Are they same? If not, how are they different from each other?
Upvotes: 4
Views: 190
Reputation: 30216
While it's true for single statements there IS a difference in case you define more than one variable:
String[] foo1, foo2; // both variables are of type String[]
String bar1[], bar2; // here they're not. But you really shouldn't do this, causes
// unnecessary confusion
Upvotes: 3
Reputation: 533492
Its also basically the same as
public static void main(String... args)
which is what I prefer.
Upvotes: 1
Reputation: 346260
Both have exactly the same meaning. However, the first is unconventional and should not be used, because it splits type information. It's a holdover from C.
Upvotes: 1
Reputation: 4593
They mean the same thing. The second form is generally preferred, as it puts the array declaration with the type declaration. By the way, there's nothing special about this appearing in the main() method, arrays can be declared both ways any place in your code.
Upvotes: 3
Reputation: 137312
Yes, they are the same, yet the convention is to write String[] args
as String[]
is the type.
Upvotes: 1
Reputation: 500297
There is no semantic difference between the two forms, the only difference is stylistic.
Upvotes: 5