Tuesday, March 14, 2017

Reserve Words in java.

Reserve Words

There are few words in java which are reserved for some meaning or functionality, these words in java called Reserved Worlds.

There are 53 reserve words in java.
Reserved Word Chart
























goto:-  As use of goto keywords creates several  problem in old programing language ,hence SUN people decide not to used “goto” keywords and band it in java.


const :- use final instead of const.

Note:- In java return type is mandatory for the methods , if method doesn’t return anything than its return type is void.
BUT
In c-language return type is optional and default return type is int.

Goto and const keyword is unused keyword if we try to use it than we will get CE.


enum- we use enum to define a group of named constant.
Ex:
enum month {
jan,feb,mar,…..dec;
}


Conclusion :-
  1. All reserve words in java only contain lower case-alphabets symbols
  2. In java we have only new keyword but not delete keyword because in java destruction of unused  object  is responsibility of java Garbage collector.


Things to aware:
  • strictfp but not strictFp.
  • instanceof but not instanceOf
  • extends but not extend
  • implements  but not implement
  • import  but not imports
  • synchronized but not synchronize
  • const but not consant

Identifiers in Java

Identifiers:-

         Any name in java, which can be used for identification purpose is called identifier. It can be class Name, method Name, variable Name, label name or package name.

Rules for Defining identifiers in java are:-

1)      The only valid allowed characters for java identifier are alphabet symbol, digit symbol, underscore symbol "_" and currency symbols . likes :-
‘$’-dollar ,
‘£’-pound ,
€- euro,
¥-yen.

         For Example:-

         int   x123;                         (valid)
         double  y#z;                      (in-valid because of # is not allowed character)

        double  y_z$;                     (valid)
                 
         char   _$_$_$;                   (valid)

2)      Identifier should not start with digits.
                               
                For Example:-
                                int x12number;                 (valid)
                                int num15;                        (valid)
                                int 15num;                        (in-valid)
3)      Identifier in Java are Case sensitive. its means
                 
                 For Example:-
                                String str1;
                                String Str1;
                                String STR1;

                Above all are valid and 3 different identifier (not same).

4)       There is no length limit for declaring identifier in java.
               
                 For Example:-
int     xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyzzzzzzzzzzzzzzzzz;

                above one is also valid.

Note:- But taking too long identifier name is not recommended for good programming practise because it reduces code readability.

5)       Reserved Word should not be taken as Identifier.

                                int sita;                 (valid)
                                int if;                    (in-valid)
                                int null;                (in-valid)
                                int try;                  (in-valid)

                                int String              (valid)

Above example 2, 3, 4 are in-valid because if, null, try are reserved words respectively.

Last one is valid because String is not reserved word but predefine class name.
               
Note: - we can also define Identifier as previously predefined class or interface or variable name but not recommended for Good practice of programming.

Question Based of Identifiers is:-
Q) Which are valid Identifiers?
a)      int integer;
b)      int sorry!;
c)       double 12xyx
d)      char ch#;
e)      int _try;
f)       double InstanceOf;
g)      String instanceof;
h)      String strictfb;
i)        boolean  _$95;
Q) How many identifier are in a below code?
                public class  Test {
                                public static void main(String[ ]  args){
                                                System.out.println("hello");
                                                Test t= new Test();
                                                t.start();
                                }
                                private int start(){
                                System.out.println("Started");
                                return (0);
                                }
                }