Clara Hasan
Clara Hasan

Reputation: 43

java coding confusion about String args[] and String[] args

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

Answers (6)

Voo
Voo

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

Peter Lawrey
Peter Lawrey

Reputation: 533492

Its also basically the same as

public static void main(String... args)

which is what I prefer.

Upvotes: 1

Michael Borgwardt
Michael Borgwardt

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

Mike Yockey
Mike Yockey

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

MByD
MByD

Reputation: 137312

Yes, they are the same, yet the convention is to write String[] args as String[] is the type.

Upvotes: 1

NPE
NPE

Reputation: 500297

There is no semantic difference between the two forms, the only difference is stylistic.

Upvotes: 5

Related Questions