☕ Java

Java Identifiers

An identifier is any name you give to a variable, method, class, package, or label in Java. There are strict rules about what's valid, and strong conventions about what's good. Get both right and your code becomes instantly readable.

What Is an Identifier?

An identifier is a name you define in your Java code. Every name you choose — for a class, method, variable, parameter, package, or label — is an identifier. Java has strict syntactic rules that define which identifiers are valid, and the compiler enforces them absolutely. Beyond the rules, Java's naming conventions define what's idiomatic — code that reads naturally to every Java developer.

The Rules — What Makes an Identifier Valid

The Java compiler enforces four hard rules for identifiers. Violate any one of them and your code won't compile:
Java
// Rule 1: Must start with a letter, underscore (_), or dollar sign ($)
//         Cannot start with a digit.
int age = 25;        // valid — starts with letter
int _count = 0;      // valid — starts with underscore
int $price = 100;    // valid — starts with $ (uncommon, avoid in practice)
int 2fast = 10;      // INVALID — starts with digit (compile error)

// Rule 2: After the first character, can contain letters, digits, _, $
int userName1 = 0;   // valid — digit after first character is fine
int user_name = 0;   // valid — underscore mid-name (avoid by convention)
int user-name = 0;   // INVALID — hyphen not allowed

// Rule 3: Cannot be a Java keyword or reserved word
int class = 5;       // INVALID — 'class' is a keyword
int for = 10;        // INVALID — 'for' is a keyword
int static = 0;      // INVALID — 'static' is a keyword

// Rule 4: No length limit — but keep them readable
int x = 0;                                    // valid (but too short — meaningless)
int thisIsAVeryLongVariableNameThatIsStillValid = 0;  // valid (but too long)

// Unicode is fully supported:
int café = 10;       // valid — é is a Unicode letter
int π = 3;           // valid — π is a Unicode letter
int 数 = 5;          // valid — Chinese character, Unicode letter

Valid vs Invalid — Side by Side

Test your understanding with these examples:
Java
// ── VALID IDENTIFIERS ──────────────────────────────────────────
int age;
String firstName;
double accountBalance;
boolean _isActive;
int $count;
float MAX_VALUE;
String userName2;
int i;               // single letter — fine in loops

// ── INVALID IDENTIFIERS ─────────────────────────────────────────
int 1stPlace;        // starts with digit
String my-name;      // contains hyphen
double my name;      // contains space
int class;           // Java keyword
boolean true;        // reserved literal
float @rate;         // @ is not allowed
String null;         // reserved literal

// ── TECHNICALLY VALID BUT AVOID ────────────────────────────────
int _;               // underscore alone — legal but meaningless
int $;               // dollar sign alone — legal but meaningless
int $_;              // legal, meaningless
// Note: in Java 9+, _ alone as an identifier is deprecated and
// produces a warning. In Java 21, it's been repurposed as the
// unnamed variable placeholder in pattern matching.

Case Sensitivity — Java Treats Every Case as Different

Java is strictly case-sensitive. age, Age, AGE, and aGe are four completely different identifiers. This is a frequent source of confusion for beginners.
Java
// These are four completely separate variables:
int age = 25;
int Age = 30;
int AGE = 35;
int aGe = 40;

// The JDK itself uses case-sensitivity deliberately:
String string1 = "hello";   // String = class (uppercase)
// string is not a keyword — String is the class name

// Common mistake — wrong case on class names:
arraylist<String> list = new arraylist<>();  // INVALID — ArrayList, not arraylist
system.out.println("hi");                    // INVALID — System, not system
math.sqrt(4);                                // INVALID — Math, not math

// Correct:
ArrayList<String> list = new ArrayList<>();
System.out.println("hi");
Math.sqrt(4);

Identifiers and Unicode

Java source files are Unicode, and the Java Language Specification allows any Unicode letter or digit in an identifier. In practice, Java codebases almost universally use ASCII — English letters and digits — because source code is shared across international teams and tools.
Java
// Java fully supports Unicode identifiers:
String naïve = "simple";        // valid — î is a Unicode letter
double göße = 1.8;              // valid — ö and ß are Unicode letters
int счёт = 0;                   // valid — Cyrillic letters

// Unicode escape sequences also work in identifiers:
// A is 'A' in Unicode
int Age = 25;              // same as:  int Age = 25;

// In practice: always use ASCII for identifiers
// Reason: consistency, readability, tool compatibility, team collaboration
// Exception: if your domain vocabulary is inherently non-ASCII (rare)

// You CAN use Unicode in string literals freely:
String greeting = "こんにちは";       // Japanese — totally fine in strings
String currency = "₹1,000";          // Rupee symbol — fine in strings

Naming Best Practices Beyond the Rules

The rules define what compiles. These practices define what's readable:
Java
// ── BE DESCRIPTIVE ─────────────────────────────────────────────
// DON'T:
int d;
String s;
double x;

// DO:
int daysSinceLastLogin;
String customerEmail;
double annualInterestRate;

// ── EXCEPTION: short names in tight scopes ──────────────────────
for (int i = 0; i < list.size(); i++) { }   // i is fine here
list.forEach(s -> System.out.println(s));    // s is fine in a lambda

// ── AVOID ABBREVIATIONS UNLESS UNIVERSAL ────────────────────────
// DON'T:
int usrCnt;
String cstmrNm;
double accBal;

// DO:
int userCount;
String customerName;
double accountBalance;

// Universal abbreviations that are fine:
int id;        // 'id' is universally understood
String url;    // 'url' is universally understood
int min, max;  // universally understood
HttpRequest req; // 'req' is widely understood in web context

// ── DON'T ENCODE TYPE IN THE NAME ───────────────────────────────
// DON'T (Hungarian notation — not idiomatic in Java):
String strName;
int intAge;
boolean bIsActive;

// DO:
String name;
int age;
boolean isActive;