-1

I've started learning Java and I was wondering the difference between:

public static void main (String [] args) {} 

and

public static void main (String args[]) {}

As you can see they look almost exactly the same but my question here is, should the '[]' be before the args or after the args? I have a Java beginners guide and it shows me examples with the '[]' after the args. I would be grateful if someone could tell me the correct way.

Thanks!.

Samih
  • 39
  • 6

4 Answers4

5

String args[] is an alternate (non standard) syntax for declaring an array, which is allowed as a carry over from C (java is based on C), probably to ease the transition to java from C. You hardly ever see it used.

And by the way there's a third valid signature for the main method:

public static void main (String... args) {}
Bohemian
  • 389,931
  • 88
  • 552
  • 692
1

In the public static void main(String args[])

args is an array of String type. So for declaring we do this ways String args[] or String []args

So both are same

SpringLearner
  • 13,546
  • 20
  • 71
  • 113
0

Both of them have the same effect - they both declare an array. I find the first one far more readable. The second syntax was only added to help programmers who have come to Java from C.

MD Sayem Ahmed
  • 27,838
  • 25
  • 109
  • 176
0

Both will work correctly.They represent the arguments to command line. String array

amudhan3093
  • 752
  • 9
  • 17