Is main a valid Java identifier?

后端 未结 12 1933
广开言路
广开言路 2020-12-23 02:48

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. 123

相关标签:
12条回答
  • 2020-12-23 02:58

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

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

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

    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.

    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.
    0 讨论(0)
  • 2020-12-23 03:01
    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.)

    0 讨论(0)
  • 2020-12-23 03:01

    Both main and java123 are valid identifiers, 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.

    0 讨论(0)
  • 2020-12-23 03:02

    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
    
    0 讨论(0)
  • 2020-12-23 03:03

    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.

    0 讨论(0)
  • 2020-12-23 03:06

    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;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题