Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it matter where you put the brackets in the main method?

I am currently starting to learn Java and I have been looking at some code and have seen the brackets in two different places.

These are the two ways I have seen it:

public static void main(String[] args) {

public static void main(String args[]) {

Is one of these ways correct formatting or is it simply whichever the programmer prefers?

like image 465
Tren46 Avatar asked May 27 '16 15:05

Tren46


3 Answers

Technically, it is a matter of your preference. When defining arrays, both are legal.

However, there is an overwhelming tendency and standard to put the brackets after the reference type, as in String[] args. Some code inspection tools even consider the other use of brackets as a bad convention or "code smell".

like image 106
Zircon Avatar answered Nov 20 '22 00:11

Zircon


From the Google Java style guide:

The square brackets form a part of the type, not the variable: String[] args, not String args[].

However, both are legal.

like image 39
Andy Turner Avatar answered Nov 20 '22 02:11

Andy Turner


From the JLS:

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.

It makes no difference. The two forms exist so as not to scare off C/C++ programmers from learning Java.

like image 28
sisyphus Avatar answered Nov 20 '22 02:11

sisyphus