A Name in Java program is called Identifiers. User-defined words in our java program is called Identifiers
It can be a class name or method name or variable name or label name. Example:

 
class Test{
public static void main(String arr[]){
int myVariable=56;
System.out.println("Identifier Test");
}
}

In above example Class Name{Test} ,Variable name {myVariable} ,String array name {arr} are the identifiers. These words are defined by programmer.

  • Rules for defining the identifiers-

    There are few rule which we have to follow while declaring the identifier-

    • The only allowed character in java Identifiers are-

      A - Z
      a - z
      0 - 9
      $
      _
      

    • If we are using any other character apart from above mention characters, we will get compile time error.

    • An identifier cannot start with the digit.

    • Identifiers can be any combination of digit and alphabets but not start from digit.

    • Keywords cannot be used as identifiers.

    • identifiers are case Case-Sensitive

    • There is no length limit for java identifiers but it is recommended to not use an identifier of more than 15 characters.

    • All pre-defined java class Names and interface name can be used as an identifier. But it's not recommended to use.


//program for creating valid and invalid identifiers
drag_handle
class Identifier
{
public static void main(String[] args) {
	int x=56;//valid identifier
	int Ab=54;//valid identifier;
	int _ab=45;//valid identifier;
	int $_test=11;//valid identifier;
	//int 4ab=54;  //invalid identifier// can't be start from digit
	//int [email protected]=65; //invalid //@ can't be used
	int Runnable=76;
	System.out.println("Runnable= "+Runnable); 

}
}

Output:

Runnable=76

What next?

The next topic is Comments in Java

Share this page