main 是有效的 Java 标识符吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52264638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:23:47  来源:igfitidea点击:

Is main a valid Java identifier?

javalanguage-lawyermainidentifier

提问by Gary Bak

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. )whoot

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

一种。 123java
main
C。 java1234
d. {abce
e. )whoot

He answered band got it wrong.

他回答了b并弄错了。

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

我看着这个问题,并认为这main一个有效的标识符,它应该是正确的。

We took a look at the Java specfor 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.

我们查看了标识符的 Java规范,它强化了这一点。我们还编写了一个示例程序,其中有一个名为 的变量main以及一个方法。他创建了一个书面反驳,其中包括 Java 文档参考、测试程序,但老师忽略了它并说答案仍然不正确。

Is maina valid identifier?

main有效的标识符吗?

采纳答案by rgettman

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.

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

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

The relevant section of the JLS is 3.8:

JLS相关部分是 3.8

An identifieris an unlimited-length sequence of Java lettersand Java digits, the first of which must be a Java letter.

Identifier:

    IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral

IdentifierChars:

    JavaLetter {JavaLetterOrDigit}

JavaLetter:

    any Unicode character that is a "Java letter"

JavaLetterOrDigit:

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

一个标识符是无限长序列的Java字母爪哇位,其中第一个必须是一个爪哇字母

标识符:

    IdentifierChars 但不是关键字或 BooleanLiteral 或 NullLiteral

标识符字符:

    JavaLetter {JavaLetterOrDigit}

Java信件:

    任何作为“Java 字母”的 Unicode 字符

JavaLetterOrDigit:

    任何 Unicode 字符是“Java 字母或数字”

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

字符序列main符合上述描述,不在3.9节关键字列表中

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

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

回答by Mike Nakis

mainis a valid java identifier, and the teacher is wrong.

main是一个有效的java标识符,老师错了。

The relevant documentation is in the Java Language Specification, right here:

相关文档在 Java 语言规范中,就在这里:

Chapter 3. "Lexical Structure", section 3.8. "Identifiers":

第 3 章“词法结构”,第 3.8 节。“身份标识”:

https://docs.oracle.com/javase/specs/jls/se10/html/jls-3.html#jls-3.8

https://docs.oracle.com/javase/specs/jls/se10/html/jls-3.html#jls-3.8

It says:

它说:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter... An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7), or a compile-time error occurs.

标识符是 Java 字母和 Java 数字的无限长度序列,其中第一个必须是 Java 字母......标识符不能与关键字(第 3.9 节)、布尔文字( §3.10.3),或空文字(§3.10.7),或发生编译时错误。

Which means that you can prove that it is a valid identifier either by:

这意味着您可以通过以下方式证明它是一个有效的标识符:

  • looking for it in the list of java keywords (hint: you won't find it there!) or simply by
  • using it as an identifier and observing that no compile-time error occurs.
  • 在 java 关键字列表中寻找它(提示:你不会在那里找到它!)或者只是通过
  • 使用它作为标识符并观察没有发生编译时错误。

回答by zero298

mainis perfectly valid because it, from the docs:

main完全有效,因为它来自文档

  1. Is a "sequence of Java letters and Java digits, the first of which is a Java letter"
  2. Is not a keyword
  3. Is not a boolean literali.e. "true" or "false"
  4. Is not null literal
  1. 是“Java字母和Java数字的序列,其中第一个是Java字母”
  2. 不是关键字
  3. 不是布尔文字,即“真”或“假”
  4. 不是空文字

回答by MC Emperor

As the other answers state

正如其他答案所述

mainis a valid Java identifier, as well as java1234.

main是一个有效的 Java 标识符,以及java1234.

I guess the confusing comes from the fact that the main(String[])method is often used as entry point by the JVM1. However, that doesn't mean that the token mainitself cannot be used as identifier2.

我想令人困惑的原因是该main(String[])方法经常被 JVM 1用作入口点。但是,这并不意味着令牌main本身不能用作标识符2

The specs say so, and the following declarations are also valid:

规范是这样说的,以下声明也是有效的:

  • A field:

    private int main;
    
  • A local variable:

    String main = "";
    
  • A method:

    private void main() { ... }
    
  • A class (although a class name starting with lowercase is discouraged):

    public class main { ... }
    
  • A package:

    package main;
    
  • 一个领域:

    private int main;
    
  • 局部变量:

    String main = "";
    
  • 一个方法:

    private void main() { ... }
    
  • 一个类(虽然不鼓励以小写开头的类名):

    public class main { ... }
    
  • 一袋:

    package main;
    


1: As noted in the comments, the JVM specification itself does not mandate any particular method as entry point, but the widely used javatool often uses such a method as entry point.
2: I would generally avoid creating a main method other than main(String[]).

1:正如评论中所指出的,JVM 规范本身并没有强制要求任何特定方法作为入口点,但广泛使用的java工具经常使用这种方法作为入口点。
2:我通常会避免创建除main(String[]).

回答by davidxxx

How maincould not be used as an identifier while it is used as identifier to declare the "main" method ?

main当它被用作声明“main”方法的标识符时,如何不能用作标识符?

For such a classic idiom :

对于这样一个经典的成语:

public class Foo{
   public static void main(String[] args){
   }
}

mainis not a keyword and it would probably never be a keyword in Java for obvious retro compatibility reasons.

main不是关键字,出于明显的复古兼容性原因,它可能永远不会成为 Java 中的关键字。



About the question, is maina good identifier ?

关于这个问题,是main一个好的标识符吗?

First : valid for a compiler doesn't mean necessarily good.
For example the java1234option that is proposed is also a valid identifier but that should really be avoided.

第一:对编译器有效并不意味着一定是好的。
例如java1234,提议的选项也是一个有效的标识符,但确实应该避免。

mainhas a very particularly and important meaning : it is used as the entry point method of classes and jars executed by the javacommand line.
Using mainfor a method name that doesn't fill the criteria to be used by the javacommand line would be just misleading while using it as variable name or a class name could make sense.
For example defining the class representing the entry point of an application as the Mainclass of the application is acceptable and so using it as variable name too such as :

main有一个非常特别和重要的意义:作为java命令行执行的classes和jars的入口点方法。
使用main不符合java命令行使用条件的方法名称只会产生误导,而将其用作变量名或类名可能有意义。
例如,将表示应用程序入口点的类定义为应用程序的Main类是可以接受的,因此也可以将其用作变量名,例如:

public class Main {

  public static void main(String args[]){
     Main main = new Main();
     // ...
  }      

}

In a general way, in Java, multiple characters or "words" are considered valid identifiers for the compiler but are strongly discouraged to be used in the client code (but generated code may do that : nested classes for example) as not readable and/or really misleading.

一般来说,在 Java 中,多个字符或“单词”被认为是编译器的有效标识符,但强烈不鼓励在客户端代码中使用(但生成的代码可能会这样做:例如嵌套类),因为不可读和/或真的误导。

For example this could be valid for the compiler :

例如,这可能对编译器有效:

public class Object { // 1
    public void foo() {
       ...
    }
}

public class BadChosenIdentifier {

    public static void main() { // 2
        new BadChosenIdentifier().toString(new Object());  
    }

    public void toString(Object java1234) { // 3, 4
        String _result$ = java1234 + " -> to avoid"; // 4
        System.out.println(_result$);
    }    
}

But we don't want :

但我们不想:

  • to name Objectour class as this is defined in java.lang(1).
  • to name a method main()if doesn't fill the criteria to be used by the javacommand line (2).
  • to overload the Object.toString()method (3).
  • to name our variables with _, $or any surprising/unmeaningful characters that go against the shared naming conventions (4).
  • 命名Object我们的类,因为这在java.lang(1)中定义。
  • main()如果未满足java命令行使用的条件,则命名方法(2)。
  • 重载Object.toString()方法(3)。
  • 来命名我们的变量_$或违背共享命名约定(4)任何奇怪/ unmeaningful字符。

回答by ililit

public class Main {
    private static String main;
    public static void main(String[] main) {
        Main.main = main[0];
        new Main().main(Main.main);
    }
    private void main(String main) {
        System.out.println(main);
    }
}

回答by MichaelK

This compiles fine on Java 1.8...

这在 Java 1.8 上编译得很好......

public class main {

    public String main = "main"; 

    public void main(String main) {
        System.out.println("This object is an instance of the class " + this.getClass().getCanonicalName());
        System.out.println("The value of the argument \"main\" for this call to the method \"main(String main)\" is " + main);
        System.out.println("The value of the field \"main\" is " + this.main);
    }

    public static void main(String[] args) {
        main main = new main();
        main.main(main.main + main.main);
    }
}

...and when executed produces the output:

...并在执行时产生输出:

This object is an instance of the class main
The value of the argument "main" for this call to the method "main(String main)" is mainmain
The value of the field "main" is main

回答by user1423956

Is it a valid identifier? Yes.

它是一个有效的标识符吗?是的。

Is it a good identifier? Not if you're using it for anything other than the method that starts at JVM launch.

它是一个很好的标识符吗?如果您将它用于除 JVM 启动时启动的方法之外的任何其他用途,则不会。

Is another valid identifier listed? Yes.

是否列出了另一个有效标识符?是的。

Did the test instructions say to choose the best answer?

测试说明是否说要选择最佳答案?

回答by 18107

I threw everything I could at it, and it appears to work. I'd say main is a valid identifier.

我尽我所能,它似乎奏效了。我会说 main 是一个有效的标识符。

package main;

public class main {

    static main main;
    String Main;

    main(String main) {
        Main = main;
    }

    main(main main) {
        System.out.println(main.Main);
    }

    main main(main main) {
        return new main(main);
    }

    public static void main(main...Main) {
        main:
        for (main main : Main) {
            main = (main instanceof Main) ? new main(main): main.main(main);
            break main;
        }
    }

    public static void main(String[] args) {
        main = new main("main");
        main.main(main, main);
        main = main.new Main(main) {
            main main(main main) {
                return ((Main)main).main();
            }
        };
        main.main(main);
        main.main(main,main);
    }

    abstract class Main extends main {
        Main(main main) {
            super("main");
        }

        main main() {
            main.Main = "Main";
            return main;
        }
    }
}

回答by shavar

Both mainand java123are valididentifiers, main isn't a reserved keyword so it's perfectly acceptable to use, as far as the test goes you should've gotten a point or half a point at least.

这两个mainjava123有效的标识符,主要不是保留关键字所以这是完全可以接受的使用,尽可能的去测试,你应该已经得到了至少一个点或半个点。