Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java array convention: String[] args vs. String args[]

Tags:

I am currently teaching students as a tutor programming conventions. I've told them that they can find most conventions in the Oracle Code Conventions.

In my last tutorial a student asked if:

public static void main(String args[]) 

or

public static void main(String[] args) 

is written by convention or if there is a difference. I have never seen the first version before, so I'm very sure that the second one is a convention. But I don't have a source for that.

Can you give me a source (preferably from oracle, like the page I've linked above) that makes clear which of both is convention?

Equivalence of both expressions

I know that both expressions are equivalent:

The JLS 7, p. 292 states:

An array type is written as the name of an element type followed  by some number of empty pairs of square brackets [].  

but also on p. 293:

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.  For example:     byte[] rowvector, colvector, matrix[]; This declaration is equivalent to:     byte rowvector[], colvector[], matrix[][]; 

But this doesn't help for the convention-quesiton.

So they are identical (not specs, but here is a source). They produce the same bytecode in a small example, so I'm very sure that they are also identical in praxis.

like image 465
Martin Thoma Avatar asked Nov 01 '12 10:11

Martin Thoma


People also ask

Is String [] args and String args [] same?

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.

Which is correct String [] args or String args []?

Both of them work fine if you use them in java programming. But String[] args is recommended because it makes more sense as String[], as a whole, represents an array object.

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.


2 Answers

This is not from Oracle but I think it will help.

It is from Kathy Sierra's book SCJP Sun Certified Programmer for Java 6

int[] key; int key []; 

When declaring an array reference, you should always put the array brackets immediately after the declared type, rather than after the identifier (variable name). That way, anyone reading the code can easily tell that, for example, key is a reference to an int array object, and not an int primitive.

like image 96
Abubakkar Avatar answered Sep 21 '22 05:09

Abubakkar


Oracle's Code Conventions do not explicitly state it, but in all examples they use the brackets immediately after the declared type.

In their example code (which should be considered authoritative in this context) they use:

private Object[] instanceVar3;

Also on the page detailing the initialization of variables they have this example that demonstrates the possible problems of putting the brackets behind the variable name:

int foo, fooarray[]; //WRONG!

One could be tempted to do this and think one were declaring several arrays. Althoug this syntactically correct (as brimborium pointed out in the comments), Oracle didn't put the capital letters there for nothing. Better to be safe, clear and also type less by putting the brackets behind the type to show clearly what you want to declare.

like image 41
titusn Avatar answered Sep 19 '22 05:09

titusn