Identifiers:
A name in java program is called identifier, which can be used for identification purposes. It can be a method name, variable name, class name, or label name.
Rules for defining java identifiers:
- The only allowed characters in java identifiers are a - z, A - Z, 0 - 9, $, and _. If we are using other characters, we will get a compile-time error.
- Identifiers can't start with digits.
- Java identifiers are case sensitive. Of course, java is treated as case sensitive programming language.
- There is no limit for java identifiers but it is not recommended to take too lengthy identifiers.
- We can't use reserved words as identifiers.
- All predefined java class name and interface name we can use as identifiers.
int String = 888;
int Runnable = 999;
Even this is valid, but it is not good programming practice.
Examples
Que 1) How many identifiers in the following program?
class Test{
public static void main(String args[]){
int x = 10;
}
}
Ans: Five
They are Test, main, String, args, x.
Que 2) Which of the following are valid java identifiers?
- tot_num Valid
- total# Invalid
- 123totoal Invalid
- total123 Valid
- ca$h Valid
- _$_$_$_$_ Valid
- arr@share Invalid
- Java2share Valid
- Integer Valid
- Int Valid
- int Invalid
Comments
Post a Comment