Is main a valid Java identifier?

后端 未结 12 1990
广开言路
广开言路 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: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.

提交回复
热议问题