In the world of programming, it is necessary to understand, what is an Identifier in Java. Because identifiers play a crucial role in defining and referencing various elements within a programming language. Java, being a widely used programming language, follows specific rules and guidelines for defining identifiers. In this article, we will explore what identifiers are in Java, delve into the rules governing them, provide examples of valid and invalid identifiers, and discuss reserved words in Java.
An identifier in Java is a name assigned to a program component such as a variable, method, class, or package. It acts as a unique identifier and allows programmers to refer to these components within their code. Identifiers can be composed of letters, digits, underscores, and dollar signs, with a few rules and restrictions.
For example :
public class SampleClass
{
public static void
main(String[] args)
{
double num1 =
5.5;
}
}
In the above Java code, we have 5 identifiers namely :
- SampleClass: class name
- main: method name
- String: predefined class name
- args: variable name
- num1: variable name
Java identifier rules
When it comes to defining valid Java identifiers, there are
specific rules that must be adhered to to avoid encountering compile-time
errors. These rules are also applicable to other programming languages like C
and C++.
• Identifiers can only consist of alphanumeric characters
([A-Z], [a-z], [0-9]), the dollar sign ('$'), and the underscore ('_'). For
instance, an identifier like "var1@" is considered invalid in Java
since it contains the special character '@'.
• Identifiers should not start with digits ([0-9]). For
instance, "123var" is not a valid Java identifier.
• Java identifiers are case-sensitive, meaning that
"var1" and "VAR1" are recognized as two distinct
identifiers.
• While there is no specific limit on the length of an
identifier, it is advisable to keep it within a range of 4 to 15 letters for
optimal usage.
• Reserved words cannot be used as identifiers. For
instance, the statement "int while = 20;" is invalid since
"while" is a reserved word. Java has a total of 53 reserved words.
Examples of valid identifiers :
MyVariable, MYVARIABLE, myVariable, x, x1, i1, _myvariable, $myvariable,
sum_of_array
Examples of invalid identifiers :
- My Variable // contains a space,
- 123Pakistan // Begins with a digit,
- a+c // plus sign is not an alphanumeric character,
- variable-2 // hyphen is not an alphanumeric character
- sum_&_difference // ampersand is not an alphanumeric character
Reserved Words in Java
Java includes a set of reserved words that have predefined meanings within the language. These words cannot be used as identifiers as they are already associated with specific functionalities. They can be briefly categorized into two parts: keywords(50) and literals(3). Here is a table of Java reserved words:
Reserved Words in Java |
||||
abstract |
boolean |
break |
byte |
case |
catch |
char |
class |
continue |
default |
do |
double |
else |
enum |
extends |
final |
finally |
float |
for |
if |
implements |
import |
instanceof |
int |
interface |
long |
native |
new |
null |
package |
private |
protected |
public |
return |
short |
static |
strictfp |
super |
switch |
synchronized |
this |
throw |
throws |
transient |
try |
void |
volatile |
while |
Please note that these reserved words cannot be used as
identifiers in Java. It's essential to choose alternative names that adhere to
the rules mentioned earlier.
Short Questions
Q: Are Java identifiers case-sensitive?
A: Yes, Java identifiers are case-sensitive. Uppercase and
lowercase letters are considered distinct.
Q: Can Java identifiers start with a digit?
A: No, Java identifiers must start with a letter or an underscore.
Q: Are there any length restrictions for Java
identifiers?
A: No, Java identifiers can be of any length, but it is
advisable to keep them concise and meaningful.
Q: What are reserved words in Java?
A: Reserved words in Java are predefined words that have
specific meanings and cannot be used as identifiers.
Q: How should I name my Java identifiers?
A: It is recommended to follow the CamelCase convention for
multi-word identifiers and choose names that are meaningful and descriptive.
In this article, we explored the concept of what is an identifier in Java,
and its rules, and provided examples of both valid and invalid identifiers.
We also discussed the list of reserved words in Java, highlighting their
significance and the need to avoid using them as identifiers. While in our previous article, we discussed JDK, JVM, and JRE. By following
these guidelines, you can ensure that your Java code is well-structured and
adheres to the language's specifications.