Identifiers are basically the names of variables, methods, classes, packages and interfaces. The name of variable identifiers must be written according to the rules and conventions given by the programming language. Rules are mandatory to follow while conventions are optional, but it's good to follow them. Like every programming language, java has it's own set of rules and conventions that we should follow while writing the identifiers name.
Rules and conventions for naming variables in java
The rules and conventions for naming your variables in java can be summarized as follows :
- Every variable name should start with either alphabets,
underscore (
_
) or dollar ($
) symbol. It should not start with number or any other special symbols. However the convention is to always begin the variable names with a letter, not "$
" or "_
". Conventionally the dollar sign should never be used at all. So variable names likeage, _age, $age
are valid names in java. - Variable names are case-sensitive. Names like
age, Age, aGe, agE
are different variables. These names can be used together in same program, but doing so is not a good practice. - Spaces are not allowed in the variable names. For eg.
int
bike speed = 100;
is not a valid declaration. It should be likeint
bikespeed = 100;
orint
bike_speed = 100;
etc. - Other characters apart from first character can be
alphabets, numbers,
$
, or_
characters. For eg.user_age, a12$_ge, _$1age2
etc are valid names of variables. - When choosing a name for your variables, the convention is to use full words
instead of short forms, doing so will make your code easier to
read and understand. Names like
age, height
andspeed
are more easy to understand than using short forms likea, h
ands
. - The variable name you choose must not be a keyword(reserved word)
of java. Java has defined some keywords like
int, double, float, char, if, else
etc. These keywords can not be used as your variable names. Refer java keyword table given below to see the list of all keywords in java. - Variable name should always exist in the left hand side
of assignment operator.
int
height = 5
is correct while5 =
is incorrect.int
height - One more convention is, if the name you choose consists
of only one word, use that word in lower case letters. If it
consists of more than one word, use first letter of each
subsequent word as capital letter. The variable names like
userName, minHeight
andmaxHeight
are good examples of this convention.
What if I missed a rule while writing the name of my variable ?
If it's a rule, not convention, the compiler will catch it and will throw a compilation error while compiling the program.
List of java keywords
Table below shows the list of all java keywords that programmers can not use for naming their variables, methods, classes etc.
abstract | continue | for | new | switch |
assert | default | goto | package | synchronized |
boolean | do | if | private | this |
break | double | implements | protected | throw |
byte | else | import | public | throws |
case | enum | instanceof | return | transient |
catch | extends | int | short | try |
char | final | interface | static | void |
class | finally | long | strictfp | volatile |
const | float | native | super | while |
The keywords const
and goto
are reserved, but they are not currently used. The words true, false
,
and null
might seem like keywords, but they are actually literals, you cannot use them as identifiers in your programs.
What is literal in java programming ?
Any constant value which can be assigned to the variable or can be used in an expression is called literal/constant. For example
10, 20.5f, 'a'
, true
, "RefreshJava"
etc are literals.
How many keywords are there in Java ?
In java there are 50 keywords(reserved words). Refer the table above to see all the keywords in java.
Can keywords be used as variable names in Java ?
No, you can't use keywords as your variable name in java, doing so will result in compilation error.
Can I use java keywords in upper case as my variable names ?
Yes you can definitely use, as keywords in java are case-sensitive but that's not a good programming style. If you want you can use them along
with other words like intVal, floatVal, charValue, doubleVar
etc are valid identifier names.
Java identifier rules program
class
NamingConvention {public static void
main(String [] args) {byte
$num1=20, _num2 = 10, num3$_ = 25; System.out.println("$num1 = "
+ $num1 +", _num2 = "
+ _num2 +", num3$_ = "
+ num3$_);byte
age=20, Age = 30, agE = 40; System.out.println("age = "
+ age +", Age = "
+ Age +", agE = "
+ agE);int
For = 15, If = 30, Byte = 50; System.out.println("For = "
+ For +", If = "
+ If +", Byte = "
+ Byte);int
bike_speed = 50; System.out.println("bike_speed = "
+ bike_speed);int
minSpeed = 10, maxSpeed = 80; System.out.println("minSpeed = "
+ minSpeed +", maxSpeed = "
+ maxSpeed); } }
Output:
$num1 = 20, _num2 = 10, num3$_ = 25
age = 20, Age = 30, agE = 40
For = 15, If = 30, Byte = 50
bike_speed = 50
minSpeed = 10, maxSpeed = 80
Following are some incorrect variable naming which will result in compile time error.
int
2num1 = 40, 12num = 10;// digits not allowed as first character
int
@num = 40, &num = 10;// only $ and _ are allowed as first character
int
bike speed = 30;// Space can not be used in variable name
int
byte = 20, float = 30, for = 40;// keywords can not be used as variable names
- Writing a meaningful name for variables is a good practice, good variable names tells you, your team-mates what information is stored inside that variable.
- Dollar($) Sign and Underscore(_) can be used as first character but it's not good programming style to use it as first characters.
- If a variable needs to be used inside a local computation or in for, while, do while loops, we can prefer names like i, j, k, a, b, c etc.