Java Identifiers
✅ Java Identifiers
In Java, identifiers are names given to variables, methods, classes, packages, or any user-defined items in a program.
Example identifiers in a program:
🧠 Rules for Java Identifiers
Identifiers must follow these rules:
| Rule | Example |
|---|---|
✔ Must start with a letter, underscore _, or dollar sign $ |
name, _value, $price |
| ✔ Can contain letters, digits, underscores, and $ | age1, student_name, $totalAmount |
| ❌ Cannot start with a number | 1age ❌ |
❌ Cannot contain special characters except _ or $ |
my-name, total#value ❌ |
| ❌ Cannot be a Java keyword | class, public, int ❌ |
🏷 Java is Case Sensitive
Identifiers treat uppercase and lowercase letters differently.
Examples:
Here, value and Value are two different identifiers.
✨ Examples of Valid Identifiers
❌ Examples of Invalid Identifiers
| Identifier | Reason |
|---|---|
2name |
Starts with a number |
student-name |
Contains - (not allowed) |
total amount |
Contains space |
class |
Reserved keyword |
📌 Naming Conventions (Best Practice)
Not rules, but recommended by Java community:
| Type | Convention | Example |
|---|---|---|
| Class Name | PascalCase | EmployeeDetails |
| Variable / Method | camelCase | studentName, getSalary() |
| Constant | UPPERCASE_WITH_UNDERSCORE | MAX_SPEED, PI_VALUE |
