Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

coding style - Java array convention: String[] args vs. String args[]

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...