A name in java program is called Identifier. It may be class name, method name, variable name and label name.
Example :-
class Test{
public static void main(String[] args){
int x= 10;
}
}
In above example all the underlined words are called identifiers in java.
Rules to define java identifier :-
Rule 1 : The only allowed characters in java identifier are :
1. a to z 2. A to Z
3. 0 to 9 4. _(Underscore)
5. $
Rule 2 : If we are using any other character we will get compile time error.
1. total_number -------- valid
2. Total# ----------------Invalid
Rule 3 : Identifiers are not allowed to start with digit.
1. ABC123 ----------valid
2. 123ABC ---------Invalid
Rule 4 : java identifiers are case sensitive up course java language itself treated as case sensitive language.
public class CaseSensitiveDemo {
public static void main(String[] args) {
int num=10;
int Num=20;
int nUm=30;
int nuM=40;
int NUM=50;
System.out.println(num);
System.out.println(Num);
System.out.println(nUm);
System.out.println(nuM);
System.out.println(NUM);
}
}
Rule 5 : There is no length limit for java identifiers but it is not recommended to take more than 15 lengths.
Rule 6 : We can't use reserved words as identifiers.
Example : int if = 10 ; --------- Invalid
Rule 7 : All predefined java class names and interface names we can use as identifiers .
Example :
public class IdentifierRule7 {
public static
void main(String[] args) {
int String =
60;
System.out.println(String);
}
}
Output :
60
Note : Although it is legal to use class names and interface names as identifiers but it is not a good programming concept.
0 Comments