🟩 1. COMMENTS in Java
Comments are used to make code more readable, explain what it does, or temporarily disable code. They are ignored by the Java compiler during execution.
✅ Types of Comments:
1. Single-line Comment (//)
Used for short explanations or notes.
2. Multi-line Comment (/* */)
Used when the comment spans multiple lines.
3. Documentation Comment (/** */)
Used to generate external documentation (API docs) using tools like javadoc. It’s written before class, method, or interface declarations.
🟨 2. IDENTIFIERS in Java
Identifier Rules in Java:
1. Identifiers can contain only:
- Letters (A-Z, a-z)
- Digits (0-9)
- Special characters: $ (dollar sign) and _ (underscore)
2. Identifiers **cannot start** with a digit.
- Example: 1name → ❌ Invalid
- Example: name1 → ✅ Valid
3. Identifiers cannot contain **any special characters** other than $ and _.
- Example: na#me → ❌ Invalid
4. Identifiers **cannot contain spaces**.
- If needed, use underscore `_` as a connector.
- Example: first_name → ✅ Valid
5. Java is **case-sensitive**.
- Example: `Name` and `name` are different identifiers.
6. Identifiers **cannot be Java keywords or reserved words**.
- Example: `int`, `class` → ❌ Invalid
- But you can use predefined class/variable/method names, if referred properly using packages.
7. From Java 9 onwards, a single underscore `_` **is not allowed** as an identifier as it became a keyword.
Note:
- There's **no limit** on identifier length in Java (unlike C).
- But using long identifiers is **not recommended** for readability and maintainability.
*/
// Java program demonstrating Identifier Rules
public class IdentifierRulesDemo {
public static void main(String[] args) {
// Valid identifiers
int _age = 25;
int $salary = 50000;
int first_name = 10;
int age1 = 30;
int Age = 35; // Different from age due to case sensitivity
// Invalid identifiers (Uncommenting will cause compile-time errors)
// int 1number = 100; // ❌ Cannot start with digit
// int first name = 20; // ❌ Cannot contain space
// int na#me = 50; // ❌ Cannot contain special character #
// int int = 60; // ❌ Cannot use reserved keyword
// int _ = 5; // ❌ Invalid from Java 9 onwards
System.out.println("All valid identifiers compiled and executed successfully!");
}
}
An identifier is the name used to identify:
-
Variables
-
Methods
-
Classes
-
Packages
-
Interfaces
✅ Rules for Naming Identifiers:
| Rule | Description | Example |
|---|---|---|
| 1. Allowed characters | Letters (A-Z, a-z), digits (0–9), _ and $ only | studentName, roll_no, $_count |
| 2. Must not start with a digit | Cannot begin with 0–9 | 1name ❌ |
| 3. No special characters | Except _ and $, no @, #, ! etc. | user#id ❌ |
| 4. No spaces | Use _ to separate words | first name ❌ → first_name ✅ |
| 5. Case-sensitive | age and Age are different | score, Score → separate identifiers |
| 6. Cannot use keywords | e.g. int, class, public, new etc. | int = 5; ❌ |
7. _ is reserved from Java 9+ | Single underscore cannot be used alone | int _ = 5; ❌ |
🔷 Examples of Valid Identifiers:
🔴 Examples of Invalid Identifiers:
📘 Best Practices for Naming Identifiers
| Element | Convention | Example |
|---|---|---|
| Class | PascalCase | StudentInfo, BankAccount |
| Method | camelCase | getDetails(), calculateTotal() |
| Variable | camelCase | totalMarks, isVerified |
| Constant | UPPER_SNAKE_CASE | MAX_LIMIT, PI_VALUE |
| Interface | PascalCase | Runnable, Serializable |
| Package | lowercase with dots | com.company.project |
🔁 Comparison Summary
| Feature | Comments | Identifiers |
|---|---|---|
| Purpose | Explain or document code | Name programming elements like variables |
| Types | Single-line, Multi-line, Javadoc | Variable, Method, Class, Interface names |
| Compiler | Ignored | Used in compilation |
| Syntax | //, /* */, /** */ | Custom names following rules |
| Visibility | Not part of code logic | Directly affects program behavior |