23

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.

Community
  • 1
  • 1
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • How doesn't it help? Doesn't it precisely say that the square brackets can appear after the type or after the variable, and that it makes no difference? – JB Nizet Nov 01 '12 at 10:35
  • 2
    BTW I usually use `public static void main(String... args)` ;) – Peter Lawrey Nov 01 '12 at 10:38
  • Note: since Java 1.5 also this is valid for main methods: public static void main(String... args) – Puce Nov 01 '12 at 10:40
  • 1
    @JBNizet The OP asks about the conventions not what is the correct syntax. – Puce Nov 01 '12 at 10:41
  • 1
    @assylias No, I don't think it's a duplicate of the linked question. – Puce Nov 01 '12 at 10:44
  • Nice blog entry from an SO'er about the document you point to: http://nurkiewicz.blogspot.co.uk/2012/10/java-coding-conventions-considered.html – assylias Nov 01 '12 at 10:50
  • 1
    @Puce: if both are correct, but everyone uses only one kind of the accepted syntax, it's a de facto convention, isn't it? That's the definition of a convention. – JB Nizet Nov 01 '12 at 11:01
  • @JBNizet No, a convention is a document that describes how to do something (although it could be done otherwise as well). It has nothing to do with how many persons are following this convention. – brimborium Nov 01 '12 at 12:51
  • 1
    @brimborium: from http://dictionary.reference.com/browse/convention: *5. a rule, method, or practice established by usage*. Everybody *uses* this notation, so it's a convention. – JB Nizet Nov 01 '12 at 13:02
  • @JBNizet from wikipedia: *Often the word refers to unwritten customs shared throughout a community.* So you are correct and I take everything back. ;) – brimborium Nov 01 '12 at 13:05

4 Answers4

20

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.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
2

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.

titusn
  • 1,201
  • 1
  • 12
  • 43
  • What do you mean by "wrong syntax"? The compiler has no problems with it... otherwise good answer. – brimborium Nov 01 '12 at 12:49
  • 1
    @brimborium: Thanks for pointing that out, I corrected the answer. I did assume it was incorrect, because of the comment added by Oracle. – titusn Nov 01 '12 at 13:10
  • You are right, that `//WRONG!` is missleading. It should be `//AVOID!` or something like that. – brimborium Nov 01 '12 at 13:14
0

There is one obscure use case for the late brackets:

int x, xs[], xxs[][];

How much this is useful, I let the reader be the judge.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

One advantage of using [] immediately after array type is: If you want to declare multiple arrays then you to write like: int[] a,b,c; But if you use [] after the array name, then we have to use[] after every array variable like: int a[],b[],c[];

user1788048
  • 265
  • 1
  • 8
  • 18