Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 254
Jérôme Verstrynge Avatar asked May 13 '11 20:05

Jérôme Verstrynge


People also ask

What is the difference between String and String [] in Java?

String[] and String... are the same thing internally, i. e., an array of Strings. The difference is that when you use a varargs parameter ( String... ) you can call the method like: public void myMethod( String... foo ) { // do something // foo is an array (String[]) internally System.

What is the difference between public static void main String [] args and public static void main String args [])?

There is no difference between the two, aside from personal preference.

What does Main String [] args mean?

String[] args means an array of sequence of characters (Strings) that are passed to the "main" function. This happens when a program is executed. Example when you execute a Java program via the command line: java MyProgram This is just a test. Therefore, the array will store: ["This", "is", "just", "a", "test"]


1 Answers

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.

like image 182
mmx Avatar answered Sep 19 '22 21:09

mmx