Sunday, 14 August 2016

Identifiers in Java (day 1)

IDENTIFIERS:

A name in java program is called Identifier which can be used for Identification purpose,it can be method name or variable name,class name or label name.


EXAMPLE:

class Shop {
public static void main(String[] args) {
int a=1;
}
}

In above example there are 5 Identifiers are there.They are
1.Shop - Name of a class
2.main - Name of a method
3.String - Name of a predefined class
4.args - Name of the array
5.a - Name of a variable

Rules for defining Java Identifiers:
1.The only allowed characters in java Identifiers are
    a-z, A-Z, 0-9, $, _
If you are using any other characters we will get compile time Error.
2.Identifiers cannot start with digit.
   Ex:  total123 - valid, 123total - Invalid
3.Java Identifiers are Case Sensitive ofcourse Java language is treated as Case sensitive                      programming language.

Ex: class Test {
       int number=10;
       int Nmuber=20;
       int NUMBER=30;
       }
we can differentiate with respect to case in above example.
4.There is no length limit for Java Identifiers but it is not recommended to take too lengthy Identifiers.
5.We can't use Reserved words as Identifiers.
   Ex: int x=10; - valid, int if=10; - invalid because if is a reserved keyword
6.All predefined Java Class names & Interface Names we can use as Identifiers
   Ex: class Test{
          public static void main(String[] args) {
          int String=10;   (valid)
          int Runnable=20; (valid)
          System.out.println(String);
          System.out.println(Runnable);
           }
      the above program successfully executed without any errors,Even though it is valid  but it is not a  good programming practise because it reduces readability and creates confusion.

Q)which of the following are Java Identifiers?
    a)total_number
    b)total#
    c)ca$h  
    d)total123
    e)123total
    f)_$_$_$
    g)all@hands
    h)Integer
    i)int
    j)Int
    k)Java2Share

A)a,c,d,f,h,j,k   are all valid Identifiers.
        






No comments:

Post a Comment

If you Like my blog Spread it and help friends for whom this blog is useful for their career.