Tuesday, March 14, 2017

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);
                                }
                } 

No comments:

Post a Comment