The Wayback Machine - https://web.archive.org/web/20251213164212/https://www.geeksforgeeks.org/java/java-identifiers/
Open In App

Java Identifiers

Last Updated : 06 Oct, 2025
Comments
Improve
Suggest changes
732 Likes
Like
Report

An identifier in Java is the name given to Variables, Classes, Methods, Packages, Interfaces, etc. These are the unique names used to identify programming elements. Every Java Variable must be identified with a unique name.

Java
class Geeks {
    public static void main { 
        int x = 9; 
    }
}

The image below describes Identifiers in this program:

class_name-

Rules For Naming Java Identifiers

There are certain rules for defining a valid Java identifier. These rules must be followed, otherwise, we get a compile-time error. These rules are also valid for other languages like C and C++. 

  • The only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), '$'(dollar sign) and '_' (underscore). For example, "geek@" is not a valid Java identifier as it contains a '@', a special character.
  • Identifiers should not start with digits([0-9]). For example, "123geeks" is not a valid Java identifier.
  • Java identifiers are case-sensitive.
  • There is no limit on the length of the identifier, but it is advisable to use an optimum length of 4 - 15 letters only.
  • Reserved Words can't be used as an identifier. For example, "int while = 20;" is an invalid statement as a while is a reserved word.

Note: Java has 53 reserved words (including 50 keywords and 3 literals), that are not allowed to be used as identifiers.

Examples of Valid Identifiers

MyVariable
MYVARIABLE
myvariable
x
i
x1
i1
_myvariable
$myvariable
sum_of_array
geeks123

Examples of Invalid Identifiers

My Variable // contains a space
123geeks // 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

Related Article:

Suggested Quiz
4 Questions

Which of the following is a valid Java identifier?

  • A

    1var

  • B

    var_1

  • C

    var-1

  • D

    var 1

Explanation:

In Java, a valid identifier:

Can include letters (a–z, A–Z), digits (0–9), underscores (_), and dollar signs ($)

Cannot start with a digit

Cannot contain spaces or special characters like.

So:

  • 1var ❌ → starts with a digit (invalid)
  • var_1 ✅ → valid (starts with a letter, contains underscore)
  • var-1 ❌ → contains hyphen - (invalid character)
  • var 1 ❌ → contains space (invalid)

Which rule is true for Java identifiers?

  • A

    They can start with a digit

  • B

    They can contain spaces

  • C

    They can contain letters, digits, underscores, and $

  • D

    Keywords can be used as identifiers

Explanation:


Which of these is NOT a valid identifier in Java?

  • A

    _counter

  • B

    $amount

  • C

    total#sum

  • D

    myVariable

Explanation:


What is the correct statement about Java identifiers?

  • A

    Identifiers are case-insensitive

  • B

    Identifiers must be written in uppercase only

  • C

    Identifiers are case-sensitive

  • D

    Identifiers must match the file name

Explanation:


Image
Quiz Completed Successfully
Your Score :   2/4
Accuracy :  0%
Login to View Explanation
1/4 1/4 < Previous Next >

Article Tags :

Explore