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?
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".
From the Google Java style guide:
The square brackets form a part of the type, not the variable:
String[]
args, notString args[]
.
However, both are legal.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With