Is main a valid Java identifier?

后端 未结 12 1934
广开言路
广开言路 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 03:07

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

    For such a classic idiom :

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

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


    About the question, is main a good identifier ?

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

    main has a very particularly and important meaning : it is used as the entry point method of classes and jars executed by the java command line.
    Using main for a method name that doesn't fill the criteria to be used by the java command 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 Main class of the application is acceptable and so using it as variable name too such as :

    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.

    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 Object our 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 java command 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).
    0 讨论(0)
  • 2020-12-23 03:07

    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?

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

    main is perfectly valid because it, from the docs:

    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 literal i.e. "true" or "false"
    4. Is not null literal
    0 讨论(0)
  • 2020-12-23 03:13
    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-23 03:20
    1. Should be single word. That is spaces are not allowed.

      Example: mangoprice is valid but mango price is not valid.

    2. Should start with a letter (alphabet) or underscore or $ symbol.

      Example: price, _price and $price are valid identifiers.

    3. Should not be a keyword of Java as keyword carries special meaning to the compiler.

      Example: class or void etc.

    4. Should not start with a digit but digit can be in the middle or at the end.

      Example: 5mangoescost is not valid and mango5cost and mangocost5 are valid.

    5. Length of an identifier in Java can be of 65,535 characters and all are significant. Identifiers are case-sensitive. That is both mango and Mango are treated differently. Can contain all uppercase letters or lowercase letters or a mixture.

    IDENTIFIER: they are class names, method names, variable names ...

    As main is not a reserved word and according to the explanation above for defining an identifier main is a valid identifier and java1234 also.Remaining options are not valid because of the above explanation.

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

    As the other answers state

    main is a valid Java identifier, as well as 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 main itself cannot be used as identifier2.

    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;
      

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

    0 讨论(0)
提交回复
热议问题