Why Identifiers ?

There must be something in this real world through which any object is identified. Suppose, if we do not have any name then no one can point us or call us or recognize us. Therefore, in programming language also we need identifiers for some identification purpose.

What is Identifier ?

  • In java identifiers is a name which is used for some identification purpose.
  • Identifiers may be class name, method name, variable name, interface name, package name or even label name.
  • In java identifier is anything used for name for any declared entity.

For example:

In the above code we have 5 identifiers. They are as under:

  • Sample : User defined class name
  • main : method name
  • String : predefined class of java
  • args : variable name of the array
  • x : variable name

Rules for Identifiers in Java ?

  • The only allowed characters are: A to Z, a to z, 0 to 9, $ and underscore ( _ ) . Other than, these characters we will get compilation error.
  • Should start with a letter (alphabet) or underscore or $ symbol.
  • Should not start with a digit but it can used in the middle or at the end.
  • Identifiers are case-sensitive. It means uppercase and lowercase letters are distinct.
  • Identifiers should not be a keyword of Java.
  • Must not contain any white spaces.
  • There is no length limit for java identifiers, but it is not recommended to take too lengthy identifiers because it reduces readability of the program.
Some valid Identifiers are:
  • demo
  • $exam
  • _test
  • sum_of_product
  • PARAM100
  • java2success
  • Int
  • Integer
Some of invalid Identifiers are:
  • 2javasuccess // starts with a digit
  • Sum of product // contains white spaces
  • x+y // + sign is not an alphanumeric character
  • sum_&_product // & sign is not an alphanumeric character
  • varaible-22 // – sign is not an alphanumeric character
  • if // keyword cannot be used as identifier name
  • int

NOTE: Identifiers must be meaningful, short enough to be quickly and easily typed and should be readable.

All predefined java class name and interface name can be used as identifier name, but it is not recommended to use because it reduces the readability of the program and creates a lot of confusion. For example:

It is not a good programming practice

Let’s see another example in case of using interface as identifier name

class Identifier { public static void main(String args[]) { int Runnable=10; System.out.println(Runnable); } }

Output : 10

In the above program we have seen that we can use interface name as an identifier, but it not recommended because it is not a good programming practice and at the same time it also reduces the readability of the program.