Identifiers in JAVA

  • All the java components like  classes,interfaces,methods,variables,packages etc needs names in order to make benefit from them in our program .These names are called “identifiers”.
  • They are used to refer the actual reference value assigned to any variable etc.
  • Example:

 If i declare
String name=”India”;
int value=10;

Here name is a identifier of a class called java.lang.String;
Here value is an identifier which represents a primitive data type of int .It is having 10 as it’s value.

  • In the HelloWorld program, HelloWorld,String- HelloWorld, args, main and println are respresents methods. 

 

Legal Identifiers:

The set of rules of the compiler to determine whether a name is legal or not.Legal identifiers are must be composed of following combinations.They are

  • Numbers(0-9)
  • Currency Symbols($ as JAVA supports)
  • Connecting Characters(_)
  • Unicode characters(A_Z ,a-z,$,_)

Example:

public class Identifiers{
public static void main(String[] args)throws Exception{
int _a=10;
int _________abc_bc=10;
int abc_____455_dfj=10;
int a#=10;
int a$=10;
int $res=10;
int &iop=10;
int 34_u=10;
int char=10;
System.out.println(_a);
System.out.println(_________abc_bc);
System.out.println(abc_____455_dfj);
System.out.println(a#);
System.out.println(a$);
System.out.println($res);
System.out.println(&iop);
System.out.println(34_u);
System.out.println(char);
}

output:

D:\OCJP>javac Identifiers_Test.java
Identifiers_Test.java:6: error: illegal character: ‘#’
int a#=10;
^
Identifiers_Test.java:9: error: not a statement
int &iop=10;
^
Identifiers_Test.java:9: error: ‘;’ expected
int &iop=10;
^
Identifiers_Test.java:10: error: illegal underscore
int 34_u=10;
^
Identifiers_Test.java:10: error: not a statement
int 34_u=10;
^
Identifiers_Test.java:11: error: not a statement
int char=10;
^
Identifiers_Test.java:11: error: ‘;’ expected
int char=10;
^
Identifiers_Test.java:11: error: not a statement
int char=10;
^
Identifiers_Test.java:11: error: ‘;’ expected
int char=10;
^
Identifiers_Test.java:15: error: illegal character: ‘#’
System.out.println(a#);
^
Identifiers_Test.java:15: error: illegal start of expression
System.out.println(a#);
^
Identifiers_Test.java:18: error: illegal start of expression
System.out.println(&iop);
^
Identifiers_Test.java:19: error: illegal underscore
System.out.println(34_u);
^
Identifiers_Test.java:19: error: ‘)’ expected
System.out.println(34_u);
^
Identifiers_Test.java:19: error: illegal start of expression
System.out.println(34_u);
^
Identifiers_Test.java:20: error: ‘.class’ expected
System.out.println(char);
^
Identifiers_Test.java:21: error: reached end of file while parsing
}
^
17 errors

so by above program we came to know that we should fallow some rules in order to make it as an identifier.

Rules of Legal Identifiers:

  • It shouldn’t start with digit(0-9).
  • It shouldn’t  start with special charactes like (@ # %^&*()!)
  • It shouldn’t contain the keyword i.e “char” “int”
  • It can have prefered length. “ppppppp______dfds_dsgfdg”.
  •  It shouldn’t contain white spaces.
  • _ can be first letter.$ can be used in midddle.Apart from no special charactes are allowd.

Now i have removed all the invalid identifier declarations .It is working fine as expected.

public class Identifiers_Test{
public static void main(String[] args)throws Exception{
int _a=10;
int _________abc_bc=10;
int abc_____455_dfj=10;
int a_=10;
int a$=10;
//int a#=10;
//int $res=10;
//int &iop=10;
//int 34_u=10;
//int char=10;
System.out.println(_a);
System.out.println(_________abc_bc);
System.out.println(abc_____455_dfj);
//System.out.println(a#);
System.out.println(a$);
System.out.println(a_);
//System.out.println(a#);
//System.out.println($res);
//System.out.println(&iop);
//System.out.println(34_u);
//System.out.println(char);
}
}

output:

D:\OCJP>javac Identifiers_Test.java

D:\OCJP>java Identifiers_Test
10
10
10
10
10

D:\OCJP>

 

Why we should follow identifier rules:

  • Conflicts like int a-=10; If we declare like this compile got confused to choose whether it is a short hand operator or identifier.