main是一个有效的Java标识符吗?

问题描述:

我的一个孩子正在高中学习Java,并在他的一个测试中得到了这个:

One of my kids is taking Java in high school and had this on one of his tests:


以下哪一个是Java中的有效标识符?

Which of the following is a valid identifier in Java?

a。 123java

b。 main

c。 java1234

d。 {abce

e。 )呐喊

他回答 b 并得到了错了。

He answered b and got it wrong.

我查看了这个问题并认为 main 是一个有效的标识符它应该是对的。

I looked at the question and argued that main is a valid identifier and that it should have been right.

我们看了一下Java spec ,它强化了这一点。我们还编写了一个示例程序,它有一个名为 main 的变量,以及一个方法。他创建了一个书面反驳,包括Java文档参考,测试程序和老师忽略它,并说答案仍然是错误的。

We took a look at the Java spec for identifiers and it reinforced that point. We also wrote a sample program that had a variable called main, as well as a method. He created a written rebuttal that included the Java documentation reference, the test program and the teacher ignored it and says the answer is still incorrect.

main 有效的标识符?

public class J {
    public static void main(String[] args)
    {
        String main = "The character sequence \"main\" is an identifier, not a keyword or reserved word.";
        System.out.println(main);
    }
}

这个编译,当执行时,会发出以下输出:

This compiles, and when executed, emits this output:

The character sequence "main" is an identifier, not a keyword or reserved word.

字符序列 main 是一个标识符,不是关键字或保留字。

The character sequence main is an identifier, not a keyword or reserved word.

JLS的相关部分是3.8


An identifier Java letters Java digits 的无限长度序列,第一个必须是 Java letter

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

标识符:

    IdentifierChars但不是Keyword或BooleanLiteral或NullLiteral

    IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral

IdentifierChars:

IdentifierChars:

    JavaLetter {JavaLetterOrDigit}

    JavaLetter {JavaLetterOrDigit}

JavaLetter:

JavaLetter:

   任何Unicode字符都是Java字母

    any Unicode character that is a "Java letter"

JavaLetterOrDigit:

JavaLetterOrDigit:

   任何Unicode字符,即Java字母或数字

    any Unicode character that is a "Java letter-or-digit"

字符序列 main 符合上述说明,不在关键字列表

The character sequence main fits the above description and is not in the keyword list in Section 3.9.

(字符序列 java1234 也是一个标识符,出于同样的原因。)

(The character sequence java1234 is also an identifier, for the same reasons.)