0

I've seen this written two different ways:

public static void main(String[] args)

and

public static void main(String args[])

Is there a difference here? Which one is preferred? Any help would be greatly appreciated.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
user2562125
  • 179
  • 1
  • 2
  • 10
  • 1
    What about: `public static void main(String...args)` – Nir Alfasi Nov 20 '13 at 22:44
  • 1
    It's a matter of preference. I personally use `String[]` because it's an array of strings, not an args array. – Jeroen Vannevel Nov 20 '13 at 22:44
  • 1
    Same question is answered [here](http://stackoverflow.com/questions/13175193/java-array-convention-string-args-vs-string-args) – Adarsh Nov 20 '13 at 22:46
  • I agree with @Jeroen. Put the [] next to what is actually an array. If you were to read it as a sentence: String array for args variable. But that's just person preference. – Michael Nov 20 '13 at 22:48
  • If you seek for array declaration in Java, this question would have never arose. Note: check link in @Adarsh comment. – Luiggi Mendoza Nov 20 '13 at 22:48

3 Answers3

5

There is no difference. I prefer String[] because I think the fact that this is an array is part of the type.

Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
2

No way is "better" than the other, as both are valid java syntaxes. You simply should be consistent, and use the one that makes most sense to you in the end. You're doing nothing more than declaring a String array called args as a parameter. Either is valid for this. I personally prefer String[] args but both are technically valid.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
1

They are equivalent. Look at this example from Java Language Specification:

The [ ] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.

byte[] rowvector, colvector, matrix[];

This declaration is equivalent to:

byte rowvector[], colvector[], matrix[][];