What is an Identifier in Java? | Reserve words in Java

In the world of programming, it is necessary to understand what is an Identifier in Java. Because identifiers are crucial in defining and referencing various elements within a programming language. Java, 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, allowing programmers to refer to these components in 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
abstractbooleanbreakbytecase
catchcharclasscontinuedefault
dodoubleelseenumextends
finalfinallyfloatforif
implementsimportinstanceofintinterface
longnativenewnullpackage
privateprotectedpublicreturnshort
staticstrictfpsuperswitchsynchronized
thisthrowthrowstransienttry
voidvolatilewhile
Table: Reserve words in Java

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.

Leave a Comment