Introduction to Identifier and Keyword In Java
A programming language is a combination of rules, symbols, and keywords. Rules are for syntax and semantics. Rules are defined in a simple, precise, to the point and formal language called metalanguage.
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 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
How does metalanguage work?
The older computer-oriented metalanguage is the Backus Naur form(BNF) developed by John Backus and Peter Naur in 1960.BNF syntax definitions are full of letters, numbers, and special symbols.
The BNF definition of an identifier in Java is as follows:
<identifier>::=<letter>|<letter-digit-sequence> <letter-digit-sequence>::=<letter-or-digit>|<letter-or-digit><letter-digit-sequence> <letter-or-digit>::<letter>|<digit> <letter>::=_|$|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z |a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z <digit>::=0|1|2|3|4|5|6|7|8|9
Where:: signifies “is defined as”,| signifies ‘or’,<> signifies encasement of the word(not terminated words [still needs to be defined] apart from these everything is terminating symbols).
So as per BNF, an identifier is a letter or letter followed by a letter digit sequence. A letter-digit-sequence is created by a <letter or a digit> then comes letter-digit sequence.
A letter or digit is defined as either a <letter> or a <digit>.
A letter is defined with the underscore,$ sign or any uppercase or lowercase letters.
A digit is defined by the numeric 0 to 9.
BNF is an extremely easy language but that simplicity gives long and difficult to read syntaxes.
There is another form of metalanguage called syntax template which shows java syntaxes. As per the syntax template, an identifier is either letter or a letter-digit combination.
- The letter can be from [A-Z],[a-z] and digit can be from [0-9].
- So an identifier in Java is of any combination of letters or sequence of letters[A-Z][a-z] digit[0-9],the underscore(_) and a dollar sign($).
- They must begin with an underscore or a dollar sign followed by optional digits.
- Since Java is case sensitive hence ‘no’ and ‘NO’ are two different entities.
- Similarly, identifiers can not start with a digit. like 24Hours,8 Hours, etc.
- No space is allowed inside identifiers like total no is an invalid identifier.
- The hyphen (-) is treated as a minus sign. So it can not be used in an identifier.
- Any other symbols are not allowed apart from the dollar ($) sign or the underscore(_) hence ?TOTAL_No or TotalNo? are not valid.
- You can not use reserved keywords as an identifier.
With the above two rules, Java has come up with the identifier and keyword rules. let’s see them in-depth:
Let us look at the 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 name,identifier
i=variable name,identifier
The rules for identifier:
- It should start with uppercase(class name) or lowercase(variable,method name)
- space is not allowed inside identifiers( get Ticket() is not valid so as Income Tax).They should be typically a single word.
- No keywords or reserved words can be used as an identifier. But can be part of Identifier(like double int is not valid but intinincome tax is valid).
- 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.
- Identifier should not start with digit[0-9] like 123incomeTax is not valid
- 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 names of the methods in java belong to the Object class which is the superclass of all the classes in java.
So if we use them in our class, it is treated as an Override of those methods. So typically these become 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 a few best practices not rule:
- Compound words or phrases 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.
- For all private and local variables, we need to use all lowercase letters combined with the 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 representing constant use of all uppercase letters and underscore in between. like TOTAL, TEMP_MAX, etc