Que: What is Identifier in JAVA?
Ans: Any name in a java program is called identifier. It can be a class name, variable name,method name,label name.
example:-
Q. How many identifiers are in the following program?
(D)
Description:-
Test,String & System are class name means 3.
main() & println()are methods means 2.
args is a reference variable of String[] means 1
Hence there are total 3+2+1= 6 identifiers..
Rules For Defining a Java Identifier
Rule 1.
a to z
A to Z
0 to 9
$ and _
example:-
total_number (valid)
total# (invalid)
If we are using other than this we will get compile time error.
Rule 2.
The identifier should not begin with a digit.
example:-
123total (invalid)
total123 (valid)
Rule 3.
Java identifiers are case sensitive of course java language itself is created as case sensitive language.
example:-
int number=10; (valid)
int Number=20 (valid)
int NUMBER=30; (valid)
Above three are different values.
Rule 4.
We can't use reserver words as identifiers.if we are trying to do then we will get compile time error.
int x=10; (valid)
int if=20; (invalid)
int else=30; (invalid)
Rule 5.
Rule 5.
All predefined java class name and interface name we can take as Identifiers.
example 1:-
example 2:-
Rule 6.
There is no length for java identifiers but it is not recommended to take lengthy identifiers.
example:-
int aaacscsvsvsscvascvncmgacvshgavg=10; (valid)
Now you have to decide which one is a valid identifier in java.
Problem Practice:-
total_number
total#
123total
total123
Ca$h
_$_$_$_$
all@hands
JavaByChetan
Integer
Int
int
Now scroll down To check your answers:-
total_number (valid)
total# (invalid)
123total (invalid)
total123 (valid)
Ca$h (valid)
_$_$_$_$ (valid)
all@hands (invalid)
JavaByChetan (valid)
Integer (valid)
Int (valid)