【发布时间】:2012-10-21 22:18:12
【问题描述】:
我目前正在以导师的身份教授学生编程约定。我告诉他们,他们可以在the Oracle Code Conventions 中找到大多数约定。
在我的上一个教程中,一个学生问:
public static void main(String args[])
或
public static void main(String[] args)
是按照惯例写的,或者如果有区别。我以前从未见过第一个版本,所以我很确定第二个是约定。但我没有那个来源。
你能否给我一个来源(最好来自 oracle,就像我上面链接的页面),以明确哪一个是约定?
两个表达式的等价性
我知道这两种表达方式是等价的:
JLS 7,p。 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[][];
但这对约定问题没有帮助。
所以它们是相同的(不是规格,但这里是source)。 它们在一个小例子中产生相同的字节码,所以我很确定它们在实践中也是相同的。
【问题讨论】:
-
顺便说一句,我通常使用
public static void main(String... args);) -
@JBNizet OP 询问约定而不是正确的语法。
-
@assylias 不,我不认为这是链接问题的重复。
-
@Puce:如果两者都是正确的,但每个人都只使用一种公认的语法,这是事实上的约定,不是吗?这就是约定的定义。
-
@brimborium:来自dictionary.reference.com/browse/convention:5。通过使用建立的规则、方法或实践。每个人都使用这个符号,所以这是一个约定。
标签: java coding-style conventions