what is an identifier in java

 Identifier: A name in java program is called identifier. It may be class name, method name, variable name and label name.

Identifiers in Java

Rules to define identifiers:

Rule 1: The only allowed characters in java identifiers are:

  • a to z
  • A to Z
  • 0 to 9
  • _
  • $

Rule 2: If we are using any other character we will get compile time error.

Example:

  • Totalamount //valid
  • Total# //invalid


Rule 3: identifiers are not allowed to start with digit.

Example:

  • ABC123//valid
  • 123ABC//InvaIid
Rule 4:  identifiers are case sensitive up course java language itself treated as case sensitive language.

Example:

class Demo {

int number=10;

int Number=20;  

int NUMBER=20;

int NuMbEr=30;

}


Rule  5: There is no length limit for java identifiers but it is not recommended to take more than 15 lengths.


Rule 6: We can't use reserved words as identifiers.

Example:

  • int if=10;//invalid
  • int public=10;//invalid
  • int static=10;//invalid
  • int i=10;//valid
  • int A=10;//valid
  • int else=10;//invalid
  • int else=10;//invalid
  • int private=10;//invalid

                                                Read more: primitive data types


Rule 7: All predefined java class names and interface names we use as identifiers.

Example 1:

class Demo

{

public static void main(String[] args){

int String=99;

System.out.println(String);

}

}

Output:

99


Example 2:

class Demo

{

public static void main(String[] args){

int System=99;

System.out.println(System);

}

}

Output:

99

Even though it is legal to use class names and interface names as identifiers but it is not a good programming practice.

 


Which of the following are valid java identifiers?

1._$_

2.CaSh

3.Java2share

4.aII@hands

5.123abc

6.TotaI#$

7.Int

8.lnteger

9.Double

10.test

11.int



Please provide the correct answer in the comment box.











Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.