top

Search

Java Tutorial

.

UpGrad

Java Tutorial

Identifiers in Java

While identifying a person we generally use a name for that person. In JAVA programming the programmer uses certain names to identify items in the program. This identification is done by a Java programmer. It allows the programmer to refer to different items while coding. Identifiers play a vital role in the Java programming language.

In Java, identifiers are symbolic names that are used for identification purposes. Identifiers can be a constant name, variable name, class name, method name, etc. By using an identifier a programmer can refer to an item from other places in the program. An identifier can be defined as a sequence of one or more characters.

All Java variables should be identified with distinct names. These unique names are known as identifiers. Identifiers can either be defined as short names or more descriptive names. Descriptive names of an identifier are preferred more to develop a logical code.

Identifiers are specified using syntax and a specific naming structure. According to the type of the identifier, the syntax varies. In Java, an integer variable name can be represented as 

int<VarName(identifier)>;

Here VarName. Is the identifier. The programmer has given a name VarName to identify the integer variable. After this, the programmer can use this variable anywhere in the program. While naming an identifier the programmer must follow some identification rule otherwise it will cause a compilation error.

Overview

This article comprises the definition of identifiers, identifiers in Java rules, and what is an identifier in Java with examples, invalid identifiers in Java, valid and invalid identifiers in Java, identifiers in javascript, keywords in Java, etc. This article will give a profound knowledge about identifiers in Java.

Rules for Identifiers in Java

Example: A basic example in Java programming to get an idea about identifiers.

public class MainClass 
{
  public static void main(String[] args) 
{
    int VarName1 = 100;
    double VarName2 = 50.0;
    System.out.println("Good Morning");
  }
}

The identifiers used in the above program are listed below.

  • MainClass is the identifier to define the class name.

  • main is the identifier to define the method name.

  • String is the identifier to define the predefined class name.

  • args is the identifier to define the string variable name.

  • VarName1 is the identifier to define the integer variable name.

  • VarName2 is the identifier to define the double variable name.

  • System is the identifier to define the predefined class name.

  • out is the identifier to define the variable name.

  • println is the identifier to define the method name.

The programmer must follow some rules while defining the identifiers in a program. If the rules are not followed then a compile-time error will be produced.

Rule 1: The identifier can only be built with regular alphanumeric characters [a -z], [A - Z], [0 - 9], and symbols that only include an underscore (_), or a dollar sign ($).

Example 1: Cricketer_89 is a valid identifier that includes only alphanumeric characters and underscore.

Example 2: Cricketer#_89 is not a valid identifier as it includes # symbol.

Rule 2: The first letter of the identifier can only be a letter (A - Z), underscore (_), or a dollar sign ($). The first letter can not be a digit [0 - 9].

Example 1: Flower_123 is a valid identifier as the first letter is an alphabet.

Example 2: 1Flower_abc is an invalid identifier as the first letter is a number.

Rule 3: While defining an identifier there should not be any space between characters.

Example 1: Dreams_123 is a valid identifier as it contains alphanumeric characters, an underscore and there is no space between characters.

Example 2: Dreams 123 is an invalid identifier because there is a space between characters.

Rule 4: Java identifiers are case-sensitive. Two identifiers of the same name but with different lowercase and uppercase characters are considered as two different identifiers.

Example: Mountain and mountain are two different identifiers.

Rule 5: The length of the identifier does not matter. However, the standard size of the identifier should be 4 – 15.

Rule 6: Java contains 53 predefined reserve keywords including int, float, static, etc. The identifier must not be a reserved keyword.

Java Reserved Keywords

Reserved keywords have predefined meanings and syntaxes for specific functionality. Java contains 53 reserve keywords. These keywords can not be used as identifiers such as class name, method name, variable name, etc. The 53 keywords which are predefined in Java are listed below.

Abstract, Asset, Boolean, break, byte, case, Catch, Char, Class, const, continue, Default, do, double, else, enum, Extends, Final, finally, float, For, Goto, If, implements, import, Interface, instanceof, int, long, native, protected, public, static, strictfp, super, short, return, synchronized, throw, This, transient, try, throws, Package, Private, switch, void, volatile, Date, while

Valid Identifiers in Java

Some examples of valid identifiers are given below.

1. School - This identifier contains alphabets.

2. School123 - This identifier contains alphanumeric characters.

3. School_123 - This identifier contains alphanumeric characters and underscore.

4. School_$ - This identifier contains alphabets, underscore, and dollar signs.

5. _School$123 – This identifier contains an underscore, alphanumeric characters, and a dollar sign.

6. school - This identifier contains lowercase alphabets.

7. SCHOOL - This identifier contains uppercase alphabets.

8. B - This identifier contains an uppercase alphabet.

9. b - This identifier contains a lowercase alphabet.

10. $ - This identifier contains a dollar symbol.

Invalid Identifiers in Java

Some examples of invalid identifiers are listed below.

1. 10Sachin@123 - This identifier starts with a numeric character and also contains @.

2. 5Sacin_123 - This identifier starts with a numeric character.

3. ^sachin-1123 - This identifier starts with a symbol ^ and also contains – symbol.

4. &Sachin - This identifier starts with a symbol &.

5. 123 - This identifier starts with a number.

6.  Sachin Not Out - This identifier contains space between characters.

7. friend123 $ - This identifier contains space between characters.

Example of Valid and Invalid Identifiers

  • Valid Identifiers

Some more examples of valid identifiers are presented below to give a clear idea about identifiers.

Example 1: 

int myVar = 50;
System.out.println(myVar);

Output:

50

Explanation: 

1. An identifier for an integer variable is created and assigned a value of 50.

2. The output is printed as 10.

Example 2: 

final double InterestRate = 5.124;
System.out.println(InterestRate);

Output: 5.124

Explanation:

1. Here InterestRate is a valid identifier that represents a double variable. The variable is created and assigned a value of 5.124.

2. The output is printed as 5.124

Example 3: 

public void Message_print(String message)
 {
    System.out.println(message);
}

printMessage("Good morning");

Output: Good morning

Explanation:

1. Here Message_print is a valid identifier that takes a string parameter message.

2. The output is printed as Good morning.

Example 4: 

public void Introduction(String message)
 {
    System.out.println(message);
}

printMessage("Hello everyone");

Output: Hello everyone

Explanation:

1. Here Introduction is a valid identifier that takes a string parameter message.

2. The output is printed as Hello everyone.

  • Invalid Identifiers

Example 1: 

int 123xyz = 5; 

Output: This will show a compilation error.

Explanation

Here 123xyz is not a valid identifier as the identifier starts with a numeric value. It is not according to the identifier rules so it shows a compilation error.

Example 2: 

int class = 20; 

Output: This will show a compilation error.

Explanation: 

In Java class is a reserved keyword and it can not be used as an identifier. So this will cause a compilation error. 

Example 3: 

int xyz@ = 5;

Output

This will show a compilation error.

Explanation

The identifier is not valid as it contains a @ symbol. So it will show a compilation error.

Example 4: 

int School abc = 10;

Output: 

This will show a compilation error.

Explanation:

The identifier is not valid as there is a space between the characters. So it will cause a compilation error.

Example 5: 

int # = 20;

Output: 

This will show a compilation error.

Explanation: 

The identifier is not valid as it contains a symbol #. So it will cause a compilation error.

Conclusion

Identifiers play an important role in the Java programming language. In Java, identifiers are symbolic names that are used for identification purposes. While programming a Java programmer should follow some rules to make a valid identifier.  A list of 53 reserved keywords is also given. One can not use a reserved keyword as an identifier. This will cause an error. In this article, clear information about the valid identifiers, invalid identifiers, and the rules used in building a valid identifier is presented. Various examples are suggested to give clear information about valid and invalid identifiers. 

FAQs

1. What is an identifier in Java?

Identifiers are symbolic names that are used for identification purposes. Identifiers can be a constant name, variable name, class name, method name, etc.

2. What is the syntax to represent an identifier in Java?

In Java, an integer variable name can be represented as 

int<VarName(identifier)>;

Here VarName. Is the identifier. The programmer has given a name VarName to identify the integer variable.

3. What is a reserved keyword?

Reserved keywords have predefined meanings and syntaxes for specific functionality. Java contains 53 reserve keywords. These keywords cannot be used as identifiers such as class name, method name, variable name, etc.

4. What are the characters used in building an identifier?

The identifier can only be built with regular alphanumeric characters [a -z], [A - Z], [0 - 9], and symbols that only include an underscore (_), or a dollar sign ($).

5. Which characters can be used as the first character of an identifier?

The first letter of the identifier can only be a letter (A - Z), underscore (_), or a dollar sign ($). The first letter can not be a digit [0 - 9].

Leave a Reply

Your email address will not be published. Required fields are marked *