Identifiers and Keywords in Java – Free Java Tutorials

Java is the among the most successful programming language. It is so because of the proper usage and declaration of fundamental which defines Java. These fundamentals include identifiers, keywords, data types and many more, and without them, any programming language cannot work. We can classify them as:

  1. Identifiers
  2. Reserved Words or Keywords
  3. Data types
  4. Literals
  5. Arrays
  6. Types of Variables
  7. Var–arc Methods
  8. Main Methods
  9. Command Line Arguments
  10. Java coding standards
  1. Identifiers:

A name in java program used for identification purpose is called an identifier. It can be a method name, variable name, class name, label name. For example:

class Student
{          
public static void main ( String [] args )
{
int x = 3;
System.out.println(“Value of x is “+x);
}
}

There are 5 identifiers in the above java program.

  • Student is the name of the class
  • main is the name of the method
  • String is the name of a predefined class in java
  • args is the name of the array
  • x is the name of the variable.

Name of labels can be-
Label-1: int y=0;
Label-2: while( i! = 0 )

Rules for defining identifiers in java which are mandatory :

  • The only allowed characters in Java identifiers are: a – z ,  A – Z , 0 – 9  , _ , $ .
    If by mistake you have used any other characters like – @. #, %,*, ^ etc the compiler will generate an error. For example:
    int check_book = 1 ;    ->     ( this is valid )
    int check@book ;             ->     ( this is invalid )
  • And also
    int check-book = 1 ;    ->     ( this is invalid )
  • Identifiers can’t start with a digit. For example:
    int 12mark = 32;          ->    ( this is invalid )
  • There is no limit for the length of an identifier but still, it is recommended to keep length small.
  • The name of Identifiers is case sensitive i.e. num and Num will be considered as two different identifiers in Java. For example:
    int num =  10 ;
    System.out.println( Num );

( This will generate an error during compilation. Since Num will be considered as a newly undefined identifier)

  • We cannot use reserved words as identifiers. For example:
    int int=10;      ->           ( this is invalid )
  • All the pre-defined interface name and class names can be used as identifiers but it is not recommended for good practice. For example:
    int String=10;
    int  Runnable= 54 ;    etc.

Here, this code will compile successfully, but it will create confusion for the programmer in future.


  1. Reserved keywords:

These are the words, which have some pre-defined functionality or meaning in a programming language. Using such words indicates the compiler to run their respective functionality.
For example:
if has a particular meaning in java and else also have some meaning. But if they are used as an identifier like-
int else = 9 ;
int if = 678 ;

Here compiler will generate an error.

There are 53 reserved words in java among which 50 are keywords ( if, else, const, goto ( unused)) and 3 are reserved literals ( true, false, null).

  • Datatype keywords are int, boolean, byte, short, long, float, double, char.
  • Flow Control keywords are if, else, switch, case, default, while, do, for, break, continue, return.
  • Access Modifiers keywords are public, private, protected, static, final, abstract, synchronized, native, strict fp, transient, volatile.
  • Exception handling keywords are try, catch, finally, throw, throws, assert.
  • Class related keywords are class, interface, implements, extends, package, import.
  • Object-related keywords are new, instanceof, super, this.
  • Other related keywords are void, goto(not used), const(replaced with final).
  • Keywords for reserved literals are true, false, null.

This is all about the identifiers and keyword in Java.

Leave a Comment

Your email address will not be published. Required fields are marked *