Identifiers in Java

A Java identifier is a name given to a packages, classes, objects, methods, variables, etc. It allows a programmer to refer to the item from other places in the program.

If you have variables that hold the name, height, and weight of a person, then choose identifiers that make their purpose obvious. That simply means, always try to use meaningful identifier rather than random alphabets so they're easier to remember.

Allowed Characters in Identifiers:

  • Uppercase Alphabet: A - Z
  • Lowercase Alphabet: a - z
  • Numbers: 0 - 9
  • Special Symbols: $, _

Rules for Identifiers

Identifiers in java program must meet these rules:

  • Java keyword cannot be used as identifiers name.
  • An identifier can begin with a letter, dollar sign, or underscore, but not a digit.
  • Digits can be used after the first character.
  • Spaces are not acceptable, so an underscore can be used instead.
  • Reserved words cannot be used.
  • The length does not matter.
  • Any other symbols or operators are not valid.

Examples

Valid Identifiers:

  • _variablename
  • _3variable
  • $testvariable
  • VariableTest
  • variabletest
  • long_but_still_valid_because_of_the_underscores
  • max_value
Invalid Identifiers:
  • 8example
  • exa+ple
  • variable test
  • class (keyword)
  • this_long_variable_name_is_not_valid_because_of_this-hyphen


Things to Remember for Java Previous Next Variables in Java