Java Identifiers

In programming languages, identifiers are used for identification purpose. In Java, an identifier can be a class name, method name, variable name or a label.

For example :

In the above java code, we have 5 identifiers namely :

  • Customer : class name.
  • main : method name.
  • String : predefined class name.
  • args : variable name.
  • customerId : variable name.
Rules for defining Java Identifiers
    1. There are certain rules for defining a valid java identifier. These rules must be followed, otherwise we get compile-time error. These rules are also valid for other languages like C,C++.

    2. The only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), ‘$‘(dollar sign) and ‘_‘ (underscore).For example “java@” is not a valid java identifier as it contain ‘@’ special character.

    3. Identifiers should not start with digits([0-9]). For example “123java” is a not a valid java identifier.

    4.Java identifiers are case-sensitive.

    5.There is no limit on the length of the identifier but it is advisable to use an optimum length of 4 – 15 letters only.

    6. Reserved Words can’t be used as an identifier. For example “int while = 20;” is an invalid statement as while is a reserved word. There are 53 reserved words in Java.

    Examples of valid identifiers :