Identifiers
Identifiers
Learn what identifiers are in programming, how they are used to name variables, methods, classes, objects, and packages, and what naming rules students must follow while writing Java programs.
What are Identifiers in Programming?
An identifier is a name given by the programmer to identify different parts of a program. Identifiers are used to name variables, methods, classes, objects, interfaces, packages, and other programming elements.
In simple words, identifiers are the names we create in a program so that we can refer to data, actions, and program structures easily.
For example, in Java, if we write int age = 20;, then age is an identifier because it is the name of a variable created by the programmer.
Easy Real-Life Example
Identifiers as Names
In a classroom, every student has a name. Instead of saying “Student 1” or “Student 2”, we use names like Ravi, Priya, or Ankit. Similarly, in programming, instead of storing values without names, we give meaningful names such as age, totalMarks, or studentName.
These names help programmers understand what the data or program element is used for.
Why are Identifiers Important?
Identifiers are important because they make programs readable, understandable, and easier to maintain. Without identifiers, it would be difficult to refer to variables, methods, and classes.
Importance of Identifiers
- Identifiers give meaningful names to program elements.
- They help programmers access variables, methods, and classes.
- They improve code readability.
- They make programs easier to understand.
- They help organize program logic clearly.
- They reduce confusion while writing large programs.
- They make debugging and maintenance easier.
- They support clean and professional coding style.
Simple Example of Identifiers
Look at the following Java program:
public class Main {
public static void main(String[] args) {
int age = 20;
String studentName = "Ravi";
System.out.println(studentName);
System.out.println(age);
}
}
In this program, several identifiers are used.
| Identifier | Type | Purpose |
|---|---|---|
Main |
Class identifier | Name of the class. |
main |
Method identifier | Name of the main method. |
args |
Parameter identifier | Name of the parameter. |
age |
Variable identifier | Stores student age. |
studentName |
Variable identifier | Stores student name. |
Identifier vs Keyword
Identifiers and keywords are different. A keyword is already defined by the programming language, while an identifier is created by the programmer.
| Keyword | Identifier |
|---|---|
| Predefined by the programming language. | Created by the programmer. |
| Has a fixed meaning. | Used as a name for program elements. |
| Cannot be changed or redefined. | Can be chosen according to naming rules. |
Examples: int, if, class |
Examples: age, total, Student |
Rules for Naming Identifiers in Java
Java has specific rules for creating valid identifiers. If these rules are not followed, the program will show a compilation error.
Identifier Naming Rules
- An identifier can contain letters, digits, underscore
_, and dollar sign$. - An identifier cannot start with a digit.
- An identifier cannot contain spaces.
- An identifier cannot contain special symbols such as
@,#,%,-, or+. - An identifier cannot be a Java keyword.
- Java identifiers are case-sensitive.
- Identifiers should be meaningful and descriptive.
- Identifiers should follow standard naming conventions.
Valid Identifier Examples
The following are valid identifiers because they follow Java naming rules.
int age;
int studentAge;
int totalMarks;
int number1;
int _count;
int $amount;
| Identifier | Why It Is Valid |
|---|---|
age |
Starts with a letter and is meaningful. |
studentAge |
Uses letters and follows camelCase style. |
totalMarks |
Meaningful and readable. |
number1 |
Contains a digit, but does not start with a digit. |
_count |
Starts with underscore, which is allowed. |
$amount |
Starts with dollar sign, which is allowed in Java. |
Invalid Identifier Examples
The following identifiers are invalid because they break Java naming rules.
int 1number;
int student age;
int total-marks;
int class;
int @value;
| Invalid Identifier | Reason |
|---|---|
1number |
Starts with a digit. |
student age |
Contains a space. |
total-marks |
Contains hyphen -, which is not allowed. |
class |
class is a Java keyword. |
@value |
Contains special symbol @. |
Java is Case-Sensitive
Java is a case-sensitive language. This means uppercase and lowercase letters are treated differently.
int age = 20;
int Age = 25;
int AGE = 30;
In the above example, age, Age, and AGE are three different identifiers.
Types of Identifiers
Identifiers can be used to name different program elements.
Variable Identifiers
Names given to variables.
Example: age, price, totalMarks
Method Identifiers
Names given to methods.
Example: calculateTotal(), displayResult(), getName()
Class Identifiers
Names given to classes.
Example: Student, Employee, BankAccount
Object Identifiers
Names used to refer to objects.
Example: student1, employeeObj, account
Package Identifiers
Names used to organize classes into packages.
Example: com.example.school, java.util
Variable Identifier Example
Variable identifiers are the most common identifiers beginners use.
int studentAge = 18;
double productPrice = 99.50;
String studentName = "Ravi";
boolean isPassed = true;
Here, studentAge, productPrice, studentName, and isPassed are variable identifiers.
Method Identifier Example
Method identifiers are names given to methods that perform actions.
public class Main {
static void displayMessage() {
System.out.println("Welcome to Java");
}
public static void main(String[] args) {
displayMessage();
}
}
Here, displayMessage is a method identifier.
Class Identifier Example
Class identifiers are names given to classes.
class Student {
int rollNumber;
String name;
}
Here, Student is a class identifier.
Object Identifier Example
Object identifiers are names used to refer to objects created from a class.
class Student {
String name;
}
public class Main {
public static void main(String[] args) {
Student student1 = new Student();
student1.name = "Ravi";
}
}
Here, student1 is an object identifier.
Naming Conventions for Identifiers
Naming conventions are not strict compiler rules, but they are professional coding practices. They help make code clean and readable.
| Program Element | Recommended Style | Example |
|---|---|---|
| Variable | camelCase | studentAge, totalMarks |
| Method | camelCase | calculateTotal(), displayResult() |
| Class | PascalCase | Student, BankAccount |
| Constant | UPPER_SNAKE_CASE | MAX_VALUE, PI |
| Package | lowercase | com.example.app |
Meaningful Identifiers
Good identifiers should describe their purpose clearly. Meaningful names make code easier to understand.
| Poor Identifier | Better Identifier |
|---|---|
a |
age |
n |
studentName |
t |
totalMarks |
x |
productPrice |
m |
monthlySalary |
Complete Java Example Using Identifiers
The following Java program uses meaningful identifiers.
public class StudentResult {
public static void main(String[] args) {
String studentName = "Ravi";
int mathMarks = 80;
int scienceMarks = 75;
int englishMarks = 85;
int totalMarks = mathMarks + scienceMarks + englishMarks;
int averageMarks = totalMarks / 3;
System.out.println("Student Name: " + studentName);
System.out.println("Total Marks: " + totalMarks);
System.out.println("Average Marks: " + averageMarks);
}
}
Output
Student Name: Ravi
Total Marks: 240
Average Marks: 80
Identifier Breakdown
| Identifier | Type | Purpose |
|---|---|---|
StudentResult |
Class identifier | Name of the class. |
studentName |
Variable identifier | Stores student name. |
mathMarks |
Variable identifier | Stores math marks. |
scienceMarks |
Variable identifier | Stores science marks. |
englishMarks |
Variable identifier | Stores English marks. |
totalMarks |
Variable identifier | Stores total marks. |
averageMarks |
Variable identifier | Stores average marks. |
How Identifiers Help Debugging
Meaningful identifiers help students debug programs faster because they make the purpose of each variable or method clear.
Debugging Questions
- Is the identifier spelled correctly everywhere?
- Is the same capitalization used consistently?
- Is the identifier declared before use?
- Is a keyword used accidentally as an identifier?
- Does the identifier name clearly describe its purpose?
- Is there any space or invalid symbol in the identifier?
- Does the identifier start with a digit?
- Are similar identifiers causing confusion?
Common Beginner Mistakes
Mistakes
- Starting an identifier with a digit.
- Using spaces inside identifier names.
- Using Java keywords as identifiers.
- Using special symbols such as
@or-. - Ignoring case sensitivity.
- Using unclear names such as
a,b, orxunnecessarily. - Using inconsistent naming style.
- Using very long and confusing names.
Better Habits
- Start identifiers with letters.
- Use meaningful names.
- Follow camelCase for variables and methods.
- Follow PascalCase for class names.
- Avoid using
$and_unless necessary. - Never use keywords as identifiers.
- Keep names readable and professional.
- Use the same spelling and capitalization consistently.
Prerequisites Before Learning Identifiers
To understand identifiers properly, students should know some basic programming concepts.
Basic Prerequisites
- Basic understanding of programming.
- Basic program structure.
- Statements in programming.
- Expressions in programming.
- Keywords in programming.
- Variables and data types.
- Simple Java syntax.
- Basic understanding of classes and methods.
Practice Activity: Identify Identifiers
This activity helps students identify identifiers in Java code.
Task
public class ProductBill {
public static void main(String[] args) {
int price = 100;
int quantity = 3;
int totalAmount = price * quantity;
System.out.println("Total Amount: " + totalAmount);
}
}
Sample Answer
| Identifier | Type |
|---|---|
ProductBill |
Class identifier. |
main |
Method identifier. |
args |
Parameter identifier. |
price |
Variable identifier. |
quantity |
Variable identifier. |
totalAmount |
Variable identifier. |
Mini Quiz
What is an identifier?
An identifier is a name given by the programmer to identify variables, methods, classes, objects, or other program elements.
Can an identifier start with a digit?
No. In Java, an identifier cannot start with a digit.
Can a Java keyword be used as an identifier?
No. Java keywords cannot be used as identifiers.
Is Java case-sensitive?
Yes. Java is case-sensitive, so age and Age are different identifiers.
Which naming style is commonly used for Java variables?
Java variables commonly use camelCase, such as studentAge and totalMarks.
Interview Questions on Identifiers
Define identifier in programming.
An identifier is a user-defined name used to identify program elements such as variables, methods, classes, and objects.
What are the rules for naming identifiers in Java?
Identifiers can contain letters, digits, underscore, and dollar sign, but they cannot start with a digit, contain spaces, use special symbols, or be Java keywords.
What is the difference between keyword and identifier?
A keyword is predefined by the language, while an identifier is created by the programmer to name program elements.
Give examples of valid identifiers.
Examples of valid identifiers are age, studentName, totalMarks, and number1.
Why should identifiers be meaningful?
Meaningful identifiers make code easier to read, understand, debug, and maintain.
Quick Summary
| Concept | Meaning |
|---|---|
| Identifier | User-defined name for a program element. |
| Variable Identifier | Name given to a variable. |
| Method Identifier | Name given to a method. |
| Class Identifier | Name given to a class. |
| Object Identifier | Name used to refer to an object. |
| Keyword | Reserved word that cannot be used as an identifier. |
| Case Sensitivity | Uppercase and lowercase letters are treated differently. |
| Naming Convention | Professional style used for naming identifiers. |
Final Takeaway
Identifiers are names created by programmers to identify program elements such as variables, methods, classes, objects, and packages. Good identifiers make programs easier to read, understand, debug, and maintain. Students should follow Java naming rules, avoid keywords, remember case sensitivity, and use meaningful names to write clean and professional code.