Identifiers:
A name in Java Program is called Identifier. which can be used for identification purpose.
it can be method name, variable name, class name or level name.
As we have seen above that there are five identifiers in this program.
As above mentioned that it can be variable(x,args), it can be method name(main), it can be class name (Simple,String)
a-z(lowercase)
A-Z(uppercase)
0-9(any digits)
_(underscore)
$
if we use another character then compiler will give error.
example:
test_name(accepted)
test$(accepted)
test#(error) only $ and _ are acceptable special characters for identifiers in Java.
2. Identifiers should not start with digits.A name in Java Program is called Identifier. which can be used for identification purpose.
it can be method name, variable name, class name or level name.
As we have seen above that there are five identifiers in this program.
As above mentioned that it can be variable(x,args), it can be method name(main), it can be class name (Simple,String)
There are following Rules for defining java identifiers:
1. Allowed character in Java identifiera-z(lowercase)
A-Z(uppercase)
0-9(any digits)
_(underscore)
$
if we use another character then compiler will give error.
example:
test_name(accepted)
test$(accepted)
test#(error) only $ and _ are acceptable special characters for identifiers in Java.
test123(accepted)
123test(error)
3.Java identifiers are case sensitive and also Java language is a case sensitive programing language. example:-
int id=0; int Id=0; int ID=0; All cases are different in java program but all acceptable.
4. There is no length limit for Java identifiers but it is not recommended to take too lengthy identifiers.
int xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=0;
(accepted but not recommended)
int mobileNumber=0;
(accepted and recommended because it is simply understandable)
5. Reserved word are not allowed for identifiers.
int x=90;(accepted)
int for=0;(not accepted)
so we can not use keyword as identifiers.
6. All Java predefine classes and interfaces name can use as identifiers.
int String=456;
int Thread=777;
but not recommended because they reduce readability and creates confusion.
which are following identifiers are valid or invalid ?
count_line(valid)
count#(invalid)
count123(valid)
123count(invalid)
count$(valid)
c_o_u_n_t(valid)
count*count(invalid)
count123value(valid)
int(invalid)
Integer(valid)
Int(valid)
$count$(valid)
1count$(invalid)
EmoticonEmoticon