Identifiers in Java

Identifiers in Java

Introduction

In Java, an identifier is the name given to a program element.
Identifiers are used to identify different parts of a Java program.

An identifier can be the name of:

  • Class

  • Method

  • Variable

  • Label

  • Interface

Choosing proper identifiers makes the program easy to read and understand.

What is an Identifier in Java?

An identifier is simply a name used to identify a variable, class, method, or any other user-defined item in a Java program.

✅ Example

int number = 10;

Here, number is an identifier.

📜 Rules to Define Java Identifiers

Java has some strict rules for naming identifiers. Let’s understand them one by one.

🔹 Rule 1: Allowed Characters

Java identifiers can contain only the following characters:

  • Letters: a to z, A to Z

  • Digits: 0 to 9

  • Underscore _

  • Dollar sign $

✅ Examples

total_number // valid
userName // valid
$amount // valid
 

🔹 Rule 2: No Special Characters Allowed

Using any character other than the allowed ones will cause a compile-time error.

❌ Examples

Total# // invalid
user-name // invalid

 

🔹 Rule 3: Cannot Start with a Digit

Identifiers must not start with a number.

✅ Valid
ABC123
number1
 
❌ Invalid
123ABC
1number
 
🔹 Rule 4: Java Identifiers are Case Sensitive

Java treats uppercase and lowercase letters as different.

✅ Example

 
class Test {
int number = 10;
int Number = 20;
int NUMBER = 30;
int NuMbEr = 40;
}

👉 All variables above are different identifiers.

🔹 Rule 5: No Length Limit

There is no maximum length for Java identifiers.

However, it is not recommended to use very long names.
👉 Try to keep identifiers short and meaningful (usually under 15 characters).

🔹 Rule 6: Reserved Words Cannot Be Used

Java keywords (reserved words) cannot be used as identifiers.

❌ Example

int if = 10; // invalid
int class = 5; // invalid

 

🔹 Rule 7: Class and Interface Names Can Be Used (But Not Recommended)

Java allows predefined class and interface names to be used as identifiers, but this is bad programming practice.

✅ Example 1

class Test {
public static void main(String[] args) {
int String = 10;
System.out.println(String);
}
}

Output:

10

 

✅ Example 2

class Test {

public static void main(String[] args) {
int Runnable = 10;
System.out.println(Runnable);
}
}

Output:

10

⚠️ Even though this is legal, it is not recommended, as it may confuse readers and developers.

✅ Good Practices for Identifiers

  • Use meaningful names

  • Follow camelCase for variables and methods

  • Use PascalCase for class names

  • Avoid using $ and _ unless required

  • Do not use predefined class names

👍 Example of Good Identifiers

 
int totalMarks;
double averageScore;
class StudentDetails { }

 

🧠 Practice Question

Which of the following are valid Java identifiers?

  1. _total

  2. 2number

  3. user$name

  4. class

  5. TotalMarks

👉 (Answer: 1, 3, and 5 are valid)

Conclusion

Identifiers are the building blocks of a Java program. Understanding the rules for naming identifiers helps you write error-free, clean, and readable code. Always follow good naming conventions to become a better Java programmer.

Learn Java step by step with EasyHaiCode 🚀
Happy Coding 😊

Java Quiz: Identifiers in Java

Quiz related to identifiers in java

1 / 10

What is an identifier in Java?

2 / 10

Which of the following can be an identifier in Java?

3 / 10

Which characters are allowed in Java identifiers?

4 / 10

Which of the following is a valid Java identifier?

5 / 10

Which rule is true about Java identifiers?

6 / 10

Which of the following identifiers are different in Java?
int number;
int Number;
int NUMBER;

7 / 10

What happens if we use a special character like # in an identifier?

8 / 10

Is there any length limit for Java identifiers?

9 / 10

Which of the following is NOT allowed as an identifier?

10 / 10

Is it legal to use predefined class names like String as identifiers?

Your score is

The average score is 100%

0%

0

Subtotal