Rules to define java identifiers
Rule 1:
The only allowed characters in java identifiers are:
a to z
A to Z
0 to 9
_ (underscore)
$ (Doller)
Rule 2:
we Can not use any other character
Example :
total_number - ✔
Total# - ✘
Rule 3:
identifiers are not allowed to starts with digit.
Example :
feelfreetocode123 - ✔
123feelfreetocode - ✘
Rule 4:
java identifiers are case sensitive up course java language itself treated as case sensitive language.
Example :
class HelloWorld {
int age=10;
int Age=10;
int aAE=10;
int AGE=10;
}
Rule 5:
There is no length limit for java identifiers
Rule 6:
We can't use reserved words as identifiers.
Example :
int break=10; - ✘
int else=10; - ✘
Rule 7:
All predefined java class names and interface names we use as identifiers.
Example :
class Test {
public static void main(String[] args){
int Integer=10;
System.out.println(Integer);
}
}
Let's Practice
INVALID - ✘
MyVariable - ✔
myvariable - ✔
My Variable - ✘
9pins - ✘
MYVARIABLE - ✔
a+c - ✘
xi_my variable
- ✔
testing1-2-3 - ✘
$myvariable
- ✔
_9pins
- ✔
andro
- ✔
O'Reilly - ✘
This_is_an_insanely_long_variable_name_that_just_keeps_going_and_going_and_going_and_well_you_get_the_idea_The_line_breaks_arent_really_part_of_the_variable_name_Its_just_that_this_variable_name_is_so_ridiculously_long_that_it_won't_fit_on_the_page_I_cant_imagine_why_you_would_need_such_a_long_variable_name_but_if_you_do_you_can_have_i
- ✔