2. Identifiers :
- A Java programmer is going to provide a name for packages, classes, interfaces, methods, variables, objects/reference and labels, these names we can say as identifiers
- If a Java programmer didn’t provide a name for packages, classes, interfaces, methods, variables, objects/reference and labels, then we will get compiler time error as : <identifier> expected.
Example :
class {}
Error : <identifier> expected.
Rules to define identifier
- While defining an identifier we should follow the below rules, if we didn't follow then it leads to compile time error.
1. Identifier should contains only
1. Alphabets (a - z and A - Z)
2. Digits (0 - 9)
3. Special Characters ( _ and $)
2. Identifier should not start with digit
- A digit can be used from second character onwards.
Example :
1StRank : error
No1Rank : It works fine
3. Identifier should not contain special characters except “ _ ”
and “ $ ”
Example :
first#name : error
first_name : It works fine
4. Identifier is case sensitive
- identifier is case sensitive
Example
a and A both letters are different
5. Reserved words cannot be used as identifier
Example
int class; // Syntax error
Note :
- we can use predefined class names as identifiers, but it is not recommended.
Example :
int String;
Note :
- There is no limit in identifier length.