Is there a difference between main(String args[]) and main(String[] args)?

JavaCoding StyleArgument Passing

Java Problem Overview


Is there a difference between:

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

and

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

I don't believe so, but I am wondering.

Java Solutions


Solution 1 - Java

Semantically, they are identical. However, I'd recommend using the latter syntax (String[] args) when declaring arrays. The former syntax is there mainly for compatibility with C syntax.

Since String[], as a whole, is the type of the object in Java, it's more consistent and clear not to split it up.

A similar question addresses the [] after method argument list.

Solution 2 - Java

There's no difference, but putting the brackets after the type (String[]) is the more common practice in Java.

Solution 3 - Java

Both of them are absolutely the same. Please refer to Java Language Specification (JLS) to see the syntax used in java.

String[] args or String args[] will create an array (reserves a place in memory) with no size and name args.

Let us Consider:

String [] x;

Putting the [] after the name avoids subtle problems like this:-

String x [] , y; //x[] is an array but y is a String
String [] x y ; //two arrays x[] and y[] both

Solution 4 - Java

Although both are used to create array of String type but second one is more popular, because with that, you can create multiple String arrays simultaneously... E.g String[] a,b; Here you have created two String Arrays a and b simultaneously.

However

String a[],b; will create String array "a" and String variable(not array) b.

Hope this clarifies.

Solution 5 - Java

No

They are just two style of writting

Solution 6 - Java

The method signature is the same so there is no difference.

Its a public method, it returns nothing, the method name is "main" and the it takes a String array.

Solution 7 - Java

Nope. There is no difference b/w the two.

See Declaring a Variable to Refer to an Array section in this doc. From that doc

> float anArrayOfFloats[]; // this form > is discouraged

so just use the second style.

Solution 8 - Java

The difference between these is that when we try to call the main method manually from one class to another by using static syntax , we use "string[] s ". At that point we have to pass an array of string type as argument in main method. When we are using "String... s" then there is no need to pass any string array as an argument. It will run and it doesn't matter if you pass the array as an argument or not (if you don't explicitly pass it, it is passed by itself) ... hence proved/////////

Solution 9 - Java

Apart from these two styles, it is worth mentioning that the following is also a valid signature for the main() method since Java 5:

public static void main(String... args)

From the Java Language specification: > The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.

The ellipsis ... is the syntax for var-args, which can be used to pass an arbitrary number of arguments to a method. More on it here.

Solution 10 - Java

String args[] and String[] args are identical. In the sense that they do the same thing, Creating a string array called args.

But to avoid confusion and enforce compatibility with all other Java codes you may encounter I'd recommend using the syntax (String[] args) when declaring arrays.

One confusion you may encounter is when you try to declare multiple String array in one line, then String[] fruits, vegetables will not be the same as String fruits[], vegetables

The former creates two string arrays fruits and vegetables, while the later creates a String array of fruits and a string variable of vegetables

Solution 11 - Java

there is a difference: For example if you write int[] arr; - here arr is the reference to the integer array whereas in case of int arr[]; - arr is a primitive int type array

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJérôme VerstryngeView Question on Stackoverflow
Solution 1 - JavammxView Answer on Stackoverflow
Solution 2 - JavaColinDView Answer on Stackoverflow
Solution 3 - Javauser3585662View Answer on Stackoverflow
Solution 4 - Javaparul popliView Answer on Stackoverflow
Solution 5 - JavajmjView Answer on Stackoverflow
Solution 6 - JavacamickrView Answer on Stackoverflow
Solution 7 - JavaBala RView Answer on Stackoverflow
Solution 8 - Javauser2133237View Answer on Stackoverflow
Solution 9 - JavaAmal KView Answer on Stackoverflow
Solution 10 - JavaMarcelView Answer on Stackoverflow
Solution 11 - JavaankitView Answer on Stackoverflow