Sunday, December 10, 2017

Java Identifiers and Reserved Words

A name in java program is called identifier which can be use for indentification purpose. It can be method name or variable name, class name or lable name.

   class Test{

public static void main(String args[]){

    int x=10;

}

   }





Rules for defining java identifiers:

   1.The only allowed character in java identifiers are a to z, A to Z, 0 to 9, $, _.
   2.Identifiers can't start with digit.
   
   if we are using any other charecter we will get compile time error.
   
   Ex: total_number - Valid
         total# - Invalid
         total123 - Valid
       123total- Invalid
   
   3. Java identifiers are case sensitive. Of course java language itself treated as case sensitive programming language.
   
   Example:
       
   class Test{
   
        int number = 10;
int Number = 20;
int NUMBER = 30;
   }
   
    In above program, we can differentiate with respect to case.
   4.There is no length limit for java identifiers. But it is not recommended to take to lengthy identifiers.
   5.We can not use reserve words for identifiers. 
   
    Example:
int x=10; // Valid
int if=20; // Invalid because if is a reserve word in java.
  
   6.All predefined java class and interface names, we can use as identifiers. Even though it is valid but it is not a good programming practice because it reduces
     readability and create confusion.
   
    Example:
   class Test{
      
  public static void main(String args[]){
  
        int String =888;
System.out.println(String); //888
  }
   
   }
   
   
   
           
   Questions for practice:

   1. Which of the following are valid java identifiers?
      total_number //valid
     total#       //valid
     123total    // invalid
     total123    // valid
     ca$h        // valid
     _$_$_$_$_$_ // valid
     all@hands   // Invalid
    Java2Share  // valid
    Interger    // valid because it is a class.
    Int         // valid
    int         // Invalid because it is reserve word.    
   
   

Reserve Words: In Java some words are reserve word to represent some meaning or functionality, such type of word are called reserve word.

Below diagram shows details of reserved words:






Keywords for data types: byte,short,int,long,float,double, boolean,char

Keywords for flow control: if,else,switch,case,default,while,do,break,for,continue,return.

Keywords for Modifiers: public,private,protected,static,final,abstract,synchronize,,native,
strictfp,transient,volatile

Keywords for exception handling: try,catch,finally,throw,throws,assert

Class related keywords: class, interface,extends,implements,package,import

Object related keywords: new, instanceof,super,this.

void return type keyword: In java return type is mandatory. If a method won't return anything, so we declare that method with void return type but in C language return type is optional and default return type is int.


Unused Keywords: There are 2 unused keywords.

       goto: Uses of goto created several problems in old languages and hence sun prople baned this keyword in java.
  
      const: Use final instead of const.
   
 Note: goto and const are unused keywords and if you are trying to use we will get compile time error.
   
Reserved Literals: There are 3 reserve literals.

     true and false : values for boolean data types
     null : default value for object reference.
 
 
enum keywords (1.5 version): we can use enum to defined a group of named constent.
Example:
 1.enum month{

 JAN, FEB,.......DEC
 }



 Conclusion: 
 1. All 53 reserved words in java contains only lower case symbols.
 2. In java we have only new keyword and there is no delete keyword because distraction of useless is responsibility of garbage collector.
 3.The following are new keywords in java:

    strictfp -- 1.2 v
    assert   -- 1.4 v
    enum     -- 1.5 v

No comments:

Post a Comment