Wednesday, 29 April 2015

Identifiers in Java

Identifier:

  •   In java identifier defined as either class name or interface name or package name or method name or variable name or block name.
  •   An identifier is a sequence of one or more characters. The first character must be a valid first character (letter, $, _).
  • It allows a programmer to refer to the item from other places in the program.

Example:

package in.blogspot.java2bigdata;
class Identifier1{
int identifier2 =10;
static{
System.out.println("inside static block");
}
public void identifier3(){
}
}

In the above example class name as Idnetifier1,variable name as identifer2, package name as in.blogspot.java2bigdata and method name as identifier3() are considered as Identifiers.


Rules for defining Identifiers in Java:

1.  Allowed char's 0 to 9,a to z,A to Z,underscore( _ ) and dollar ( $ )
     if we are using any other character we will get Compile time error.

2.Identifier should not start with digits.
       int  12number;  // compile time error
       int  number;    // valid identifier

3.Identifier should not a keyword/reserved word (like int,float,public).

4.There is a big difference between case
         int Num;
         int num;
         int NUM;
 above three identifiers are identical.
5.we can use java predefined class names as identifiers but not recommended.
    int System = 10;

6.There is no length limit for java Identifier but not recommend to take more then 15.
   int noLengthLimittttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt;


Legal identifiers examples:

String _t;
String _$;
String _2;
String _____2_u;
String $s;
String this_is_a_long_sample_of_identifier_name;
String ALL_UPPERCASE;
String all_lowercase;

Illegal identifiers examples:

String :c;
String -b;
String s#;
String .d;
String 9n;
String ?what;
String boo+!coo;


Here are some hints on how to formulate identifier name:


    Classes & Interface - The first letter should be capitalized. If the desired name is composed of several words, the first letter of the inner word should be capitalized (camelCase).

    For example: Chair, InformationDepartment and HeadOffice.

    Methods - The first letter should be lower case, and then Lower camelCase rules should be applied. Also, take note that in method names should be verb-noun pairs.

    For example: getTotal, setContactNumber and buyCellphone.
   
    Variables - same rules apply on letter case with methods. However, it should idially be a short and meaningful.
    For example: firstName, currentLocation and totalPrice.
   
    Constants - Java constants should be named using uppercase letters with underscore characters as separator. Take note that constants are created by marking variables as static and final.
    For example: PERSON_LABEL, HELLO_MESSAGE and GREETING.


    CamelCase - CamelCase (also known as Upper CamelCase) is where each new word begins with a capital letter (e.g., CamelCase, CustomerAccount, PlayingCard).
   
mixedCase - Mixed case (also known as Lower CamelCase) is the same as CamelCase except the first letter of the name is in lowercase (e.g., hasChildren, customerFirstName, customerLastName).

If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...

No comments:

Post a Comment