0

I already searched, but I couldn't find what I was looking for, so I come with a new question, hoping y'all can tell me, When should I use

public static void main(String[] args)

over

public static void main(String args[])
arshajii
  • 127,459
  • 24
  • 238
  • 287
SaraMaeBee
  • 95
  • 1
  • 1
  • 6

5 Answers5

2

There is absolutely no difference between the two, other than perhaps readability.

Creating arrays like String args[] is allowed simply to add some likeness to C/C++.

You should always prefer String[] args.

Note that there is a third option too, using varargs:

public static void main(String... args)
arshajii
  • 127,459
  • 24
  • 238
  • 287
2

Both of these are same. Use whatever you feel free. But in Java docs your second style is discouraged. Take a look I am quoting from Java doc,

You can also place the brackets after the array's name:

// this form is discouraged float anArrayOfFloats[];

taufique
  • 2,701
  • 1
  • 26
  • 40
0

They're the same, but public static void main(String[] args) is more commonly used.

Dale22599
  • 337
  • 1
  • 2
  • 7
0

Java naming conventions favor String[] args over String args[]. You should always use the former in all your code.

The reason for this is practical.

Java is a statically typed language. Every variable in Java must have a known type.

The class literal for a String arg is String.class

The class literal for a String array, eg. String[] args, is String[].class

The notation that keeps the brackets with the type name makes it immediately clear to the reader that the type is an array type (which is different from the corresponding non-array type). Although unlikely, it is possible that a naive reader would conclude that the class object for String args[] was String.class ... which is incorrect.

scottb
  • 9,908
  • 3
  • 40
  • 56
0

There is no difference between (String[] args) and (String args[]). It's just a parameter in the function. In fact,you can claim an array with different types.for example,you can claim an array of double,you can write 'double[] array' or 'double array[] '

dryairship
  • 6,022
  • 4
  • 28
  • 54
Casin
  • 1