Identifiers in Java | Java Identifiers Rules | [oracle certification for java | oracle certification ]
Before Jumping to technical definition, Let’s understand literal meaning of Identifier, what is it ?
Identifier means : "A person or things that identify something or someone".
By understanding definition of Identifier, and knowing that things are identified with it’s name, so through this, we can define Identifiers definition technically.
Definition 1 : A name that identify class, interface, variable and so on is called Identifier in java.
Definition 2 : Any name in java programming is called Identifier, it can be class name, variable name, interface name etc.
Let’s understand this with an example :
class Employee {
long employeeId;
public static void main(String args[] ){
}
}
}
In this example let’s list down all Identifiers.
1. Employee (Class name ).
2. employeeId (Variable name)
3. main (Method name)
4. args (Argument name )
So we have list down all the name which is used in the above class these all are Identifiers. So it's cleared writing any name in java class/interface is called Identifier.
Click here to know about [oracle certification | oracle certification for java]
Now we are very clear about Identifiers in Java, Next step is to know Java Identifiers Rules.
Rule 1. Only allowed characters to make Java Identifiers are :
(a – z) and (A - Z ) and ( 0 - 9 ) and ( _ & )
Example : employeeId, all_employee, _employeeId, _$_, employee1 these are the valid Identifiers
Let’s see some Invalid Identifier
all@employee, all-employee, 1employee.
Here @ and – are not allowed in java Identifier, numbers are allowed but 1employee is invalid Identifier, let's see next rule to understand this.
Rule 2. The first character in java identifier must be letter not digit-
Example
: employee1 is valid but
1employee is invalid
Identifier because Identifier must not start with number.
Rule 3. Identifiers of Java are case sensitive.
Example : employee and Employee are two different identifiers. Salary and salary, SALARY are three different identifiers.
class Employee{
long salary;
long Salary;
long SALARY;
}
In this Employee class these three variables declared are all different Identifiers, So be careful while making any Identifier in Java because of case sensitive.
Rule 4. Reserve words can not be used as Java Identifiers :
Keywords are reserve words that are predefined in language, so can not be used as Identifier.
Example : boolean, imports, static, private, this etc. These are the keywords/reserve word, can not be used as Identifier.
class Employee {
long static;
int private;
}
here static and private both are reserved keywords so using these words as Identifier will cause Compilation Error.
Rule 5. We can use predefine Java class/interface name as an Identifier :
It is allowed but not recommended to use predefine java class / interface name as Identifier
Example :
class Employee {
int Runnable = 10;
int String = 5;
}
In this example we have used predefine class String and predefine interface Runnable as an identifiers that is valid. But it’s not recommended to use predefine classes / interface as an java identifiers.
Rule 6 : There is not any rule to define length limit for java Identifier :
You can make Java Identifier with any length but according to convention it should not be more then 15 character.
Example : student_roll_number, sum_of_all_prime_numers etc. It can be of any length.
Example : student_roll_number, sum_of_all_prime_numers etc. It can be of any length.
There are some exercise based on above rule, you have to find which one is valid/Invalid Identifier
1. Which one is legal Identifiers
sum_$, $$_100, number, number1, 1number, Integer, int, all@employ, all_members_of_team.
2. Which one is Invalid Identifiers
12Employee, all-employ, all_man, Boolean, boolean, $_$, _$_, $@_
Comment your answer with explanation in comment box below
0 comments:
Post a Comment