Identifier: A name in Java program is called Identifier, which can be used for identification purpose.
It can be any of the following:
- Variable Name
- Class Name
- Label Name
Q. In the following code identify number of Identifiers present and highlight them -
class Test {
public static void main(String[] args) {
int x = 10;
}
}Identifiers:
- Test - class name
- main - method name
- String - predefined java class name
- args - name of array
- x - name of variable
Total 5 identifiers in above example
-
Characters we can use: The only allowed characters are-
a-z, A-Z, 0-9, '$', '_'
Examples: -
Identifier should mot start with digit As the rule name suggest an Identifier can't start with digit.
Examples: -
Case-Sensitive: Not just Identifiers, whole Java language is treated as Case-Sensitive programming language. Q. Is following code valid?
class Test { public static void main(String[] args) { int number = 10; int Number = 20; int NUMBER = 30; }
-
Length Limit: Even though there is no limit for java identifiers, it is not recommended to take too lengthy identifiers. Examples:
-
We can't use reserved words as identifiers:: In Java we can't use reserved words as identifiers. Examples:
-
Pre-Defined Java class names and interface as Identifiers All pre-defined Java classnames and interface can be used as Identifiers. Even though it is valid, but it is not a good programming practice, because it reduces Readability and creates confusion.
class Test { public static void main(String[] args) { int String = 888; System.out.println(String); }
class Test { public static void main(String[] args) { int Runnable = 999; System.out.println(Runnable); }
Q. Which of the following are valid Java identifiers?
| Sr. No. | Identifier | Answer |
|---|---|---|
| 1. | total_number |
|
| 2. | total# |
|
| 3. | 123total |
|
| 4. | total123 |
|
| 5. | ca$h |
|
| 6. | _$_$_$_$_ |
|
| 7. | all@hands |
|
| 8. | Integer |
|
| 9. | Int |
|
| 10. | int |