Identifier and Keyword Concept Simplified In Java



An Identifier is a meaningful name or token given to java components(classes,methods,variables,objects,labels,packages and interfaces) to identify it correctly. Java is a combination of Identifier and keywords. Keywords are the special words that express something in terms of java. Keywords are defined and reserved in java. However Identifier is kind of mixed mode,user defined and inbuilt.
Javacode=Identifiers+keywords
Let us look at a code:


public class HelloWorld{

     public static void main(String []args){
         int i=10;
        
     }
}
 
Here public,static,void,int are keywords
HelloWorld-class name,identifier
main-method mame,identifier
i=variable name,identifier

The rules for identifier:

  1. It should start with uppercase(class name) or lowercase(variable,method name)
  2. space is not allowed inside identifiers( get Ticket() is not valid so as income Tax).They should be typically single word .
  3. No keywords or reserved words can be used as an identifier.But can be part of Identifier(like double int is not valid but int intIncomeTax is valid).
  4. There no limitation of identifier length, it can start from 1 character to n characters as long as they can be identified,easily typed,descriptive and easily readable.
  5. Identifier should not start with digit[0-9] like 123incomeTax is not valid
  6. Identifiers can contain [a-z],[A-Z],[0-9] and special symbol dollar($) and underscore ( _ ) are allowed.Like-&Ticket is not valid so as %deal123 and @Tax.
Below are the reserved keywords that can not be directly an identifier.However they can be part of the identifier.
Keywords Keywords Keywords Keywords Keywords
false null true abstract do
break char default extends double
for assert byte class final
goto case const finally if
else boolean catch continue enum
implements import instanceOf int interface
long native new package private
protected public return short static
staticfp super switch synchronized this
throw throws transient try void
volatile while catch var float
byvalue cast class generic operator
outer var future inner
Below are the methods names in java belong to Object class which is the super class of all the class in java. So if we use them in our class, it is treated as Override of those methods. So typically these becomes reserved methods. We should not define them to use for our purpose.
Method name Method name Method name
clone() getClass() notifyAll()
equals() hashCode() toString()


Below are few best practices not rule:



  • Compound words or phases are used following camel casing.When two or more words are used,the second and subsequent words are written with a leading uppercase letter.like-getTicket(),incomeTax




  • Names of all public methods and instance variable start with a leading lowercase letter.




  • All private and local variables , we need to use all lowercase letters combined with underscore and of two or more letters.like name_of_student.




  • All classes and interfaces start with a leading uppercase letter and each subsequent letter of words in uppercase like-HelloWorld.java or class HelloWorld




  • Variables representating constant use all uppercase letters and underscore in between. like TOTAL,TEMP_MAX etc
  • Identifier and Keyword Concept Simplified In Java Identifier and Keyword Concept Simplified In Java Reviewed by Animesh Chatterjee on September 26, 2018 Rating: 5

    No comments:

    Powered by Blogger.