我目前作为一名编程导师正在教授学生编程约定。我告诉他们,他们可以在Oracle Code Conventions中找到大多数约定。
在我的最后一个教程中,有个学生问:
public static void main(String args[])
或者public static void main(String[] args)
如果有惯例或存在差异,则会写成第二种表述方式。我以前从未见过第一种版本,因此非常确定第二种是一种惯例。但我没有来源证明这一点。
你能给我一个来源(最好是来自Oracle的,就像我上面链接的页面)来明确说明哪个是惯例吗?
两种表达式的等价性
我知道这两种表达式是等价的:
JLS 7,第292页指出:
An array type is written as the name of an element type followed
by some number of empty pairs of square brackets [].
但也在第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[][];
但是这对于惯例问题没有帮助。
所以它们是相同的(不是规格,但这里有一个来源)。 它们在一个小例子中产生相同的字节码,所以我非常确定它们在实践中也是相同的。
public static void main(String... args)
;) - Peter Lawrey