1. The Hook (The "Byte-Sized" Intro)
- In a Nutshell: Identifiers = names (variables, methods, classes).
- Rules: Start with letter/underscore/$ (not digit), case-sensitive, no keywords. Keywords = 50+ reserved words (if, class, public, etc.), can't be used as identifiers. Contextual keywords = special meaning in context (module, var, yield).
- Best practices: Use meaningful names (camelCase for variables, PascalCase for classes), avoid single letters except loops.
- Golden rule: Code is read more than writtenβmake names self-explanatory!
Think of naming people. Identifiers = person's name (unique, meaningful). Rules = naming conventions (can't start with number "1John"). Keywords = reserved titles (can't name someone "President" as regular name). Best practices = use full names (not "J" for John)!
2. Conceptual Clarity (The "Simple" Tier)
π‘ The Analogy
- Identifier: Street address (unique location)
- Keyword: Traffic sign (reserved meaning)
- Naming Convention: Dress code (follow the rules)
3. Technical Mastery (The "Deep Dive")
java
// ===========================================
// 1. IDENTIFIERS (Valid Names)
// ===========================================
// β
VALID identifiers
int age; // Starts with letter
String user_name; // Contains underscore
double $salary; // Starts with $ (not recommended)
int _count; // Starts with underscore
String firstName123; // Contains digits (not at start)
// β INVALID identifiers
int 123name; // β Starts with digit
String first-name; // β Contains hyphen
double my salary; // β Contains space
int class; // β Reserved keyword
// Case-sensitive (different identifiers)
int value = 10;
int Value = 20;
int VALUE = 30;
// All three are DIFFERENT variables!
// ===========================================
// 2. NAMING CONVENTIONS
// ===========================================
// β
Variables and methods: camelCase
int studentAge;
String userName;
void calculateTotal() { }
double getBalance() { }
// β
Classes and interfaces: PascalCase
class Student { }
interface Runnable { }
class BankAccount { }
// β
Constants: UPPER_SNAKE_CASE
final int MAX_SIZE = 100;
final String DEFAULT_NAME = "Unknown";
static final double PI = 3.14159;
// β
Packages: lowercase.dot.notation
package com.example.myapp;
package java.util;
// β BAD naming (but technically valid)
int x; // Not descriptive
String s1; // What is s1?
void m() { } // What does m() do?
class data { } // Should be PascalCase
// β
GOOD naming
int studentCount;
String emailAddress;
void processPayment() { }
class Customer { }
// ===========================================
// 3. RESERVED KEYWORDS (Cannot use as identifiers)
// ===========================================
// 50 Reserved Keywords in Java:
// abstract continue for new switch
// assert default if package synchronized
// boolean do goto private this
// break double implements protected throw
// byte else import public throws
// case enum instanceof return transient
// catch extends int short try
// char final interface static void
// class finally long strictfp volatile
// const float native super while
// β CANNOT use these as identifiers
int if = 10; // β Compile error
String class = "Java"; // β Compile error
void return() { } // β Compile error
// ===========================================
// 4. CONTEXTUAL KEYWORDS (Java 9+)
// ===========================================
// These have special meaning in specific contexts
// but can be used as identifiers elsewhere
// β
As identifier (allowed)
String var = "test"; // var as variable name (allowed)
int module = 5; // module as variable name
String yield = "result"; // yield as variable name
// Special meaning in context:
var list = new ArrayList<>(); // var = local variable type inference
module java.base { } // module = module declaration
yield value; // yield = switch expression (Java 14+)
// ===========================================
// 5. LITERALS (Special values, not keywords)
// ===========================================
// These are literals, not keywords:
true // boolean literal
false // boolean literal
null // null literal
// Can use as identifiers (but DON'T!)
int true = 1; // β Compiles but VERY confusing!
String null = ""; // β Compiles but terrible practice!
// ===========================================
// 6. BEST PRACTICES
// ===========================================
// β
GOOD: Meaningful, descriptive names
int studentAge = 20;
String customerEmail = "user@example.com";
double accountBalance = 1500.50;
void calculateMonthlyPayment(double principal, double rate) {
// Clear what this does
}
class ShoppingCart {
// Clear what this represents
}
// β BAD: Cryptic, unclear names
int sa = 20; // What is sa?
String ce; // What is ce?
double ab; // What is ab?
void cmp(double p, double r) {
// What does cmp do? What are p and r?
}
class SC { // What is SC?
}
// Loop variables: single letters OK in short loops
for (int i = 0; i < 10; i++) { // β
OK for loop counter
System.out.println(i);
}
// But for collections, use descriptive names
for (Student student : students) { // β
Clear
System.out.println(student.getName());
}
// NOT this:
for (Student s : students) { // β Less clear
System.out.println(s.getName());
}5. The Comparison & Decision Layer
| Element | Convention | Example |
|---|---|---|
| Variable | camelCase | studentAge |
| Method | camelCase | calculateTotal() |
| Class | PascalCase | BankAccount |
| Interface | PascalCase | Runnable |
| Constant | UPPER_SNAKE_CASE | MAX_SIZE |
| Package | lowercase | com.example.app |
6. The "Interview Corner" (The Edge)
The "Killer" Interview Question: "Can you use 'var' as an identifier in Java?" Answer: Yes, but context matters!
java
// β
var as identifier (allowed)
String var = "test";
int var = 10;
// β
var as type inference (Java 10+)
var list = new ArrayList<String>(); // var infers type
var count = 10; // var infers int
// var is a "contextual keyword" (not reserved keyword)
// Special meaning: local variable type inference
// But can still be used as identifier elsewhereFollow-up: "Why is 'goto' a keyword if Java doesn't use it?"
Answer: Reserved for future use. Java reserves it to prevent confusion (C/C++ have goto). Same with const (use final instead).
Pro-Tips:
- Avoid underscores in names (except constants):
java
// β Avoid
int user_age;
String first_name;
// β
Prefer
int userAge;
String firstName;- Boolean naming (use is/has/can):
java
// β
Clear intent
boolean isActive;
boolean hasPermission;
boolean canDelete;
// β Unclear
boolean active;
boolean permission;- Method naming (verbs for actions):
java
// β
Action verbs
void calculateTotal()
String getName()
void setAge(int age)
boolean isValid()
// β Noun names
void total()
String name()