Rules of Identifiers Java

An identifier in Java can denote a variable or method or class name or package or an interface. An identifier allows the Programmer to refer the value from a different part of the program.

For example,

int magoes = 10;

Whenever, 10 is required in the code, the programmer can use the identifier mangoes.

Java comes with clear cut rules to name an identifier. Java follows the same rules of C/C++.

Rules of Identifiers Java
  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.
  6. Identifiers are case-sensitive. That is both mango and Mango are treated differently.
  7. Can contain all uppercase letters or lowercase letters or a mixture.

Note: Always choose the identifier a meaningful word. Suppose you would like to store the number of mangoes in the code, write as

int numberOfMangoes = 10;

The above statement can also be written as

int x = 10;

The problem in the above statement is x should be remembered for the number of mangoes which may be tedious if variables are many.

Pass comments and suggestions on this tutorial on "Rules of Identifiers Java".