Is main a valid Java identifier?

2019-03-07 23:06发布

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

Which of the following is a valid identifier in Java?

a. 123java
b. main
c. java1234
d. {abce
e. )whoot

He answered b and got it wrong.

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

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.

Is main a valid identifier?

12条回答
欢心
2楼-- · 2019-03-07 23:19
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);
    }
}
查看更多
Luminary・发光体
3楼-- · 2019-03-07 23:20
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 main is an identifier, not a keyword or reserved word.

The relevant section of the JLS is 3.8:

An identifier is an unlimited-length sequence of Java letters and 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"

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

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

查看更多
做自己的国王
4楼-- · 2019-03-07 23:26

This compiles fine on 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
查看更多
Luminary・发光体
5楼-- · 2019-03-07 23:26

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

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;
        }
    }
}
查看更多
够拽才男人
6楼-- · 2019-03-07 23:29

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.

Is another valid identifier listed? Yes.

Did the test instructions say to choose the best answer?

查看更多
孤傲高冷的网名
7楼-- · 2019-03-07 23:29

That teacher made a minor mistake in either assuming main is not a valid identifier or simply phrasing the question wrong. He possibly meant to say "a good identifier".
But ignoring your sons arguments and thereby discouraging his scientific approach of checking relevant literature (Java specification) and performing an experiment (writing a sample program) is the exact opposite of what a teacher is supposed to do.

查看更多
登录 后发表回答