Identifiers
An identifier is simply the name you give to something in a Java program — a variable, a method, a class, or a label. Every identifier must follow a small set of rules, and Java enforces them at compile time.
Allowed Characters
An identifier can contain letters (a-z, A-Z), digits (0-9), underscores
(_), and dollar signs ($). Any other character — a space, a hyphen, an
@, etc. — causes a compile-time error.
int total_number = 7; // valid
int $price = 100; // valid
Can't Start With a Digit
An identifier cannot begin with a digit, though digits are fine after the first character.
int 123total = 25; // invalid - starts with a digit
int total123 = 25; // valid
Identifiers Are Case-Sensitive
Java treats uppercase and lowercase letters as completely different characters, so identifiers that differ only in case refer to different variables. This trips up a lot of beginners coming from case-insensitive languages.
public class CaseSensitivity {
public static void main(String[] args) {
int number = 10;
int Number = 20;
int NUMBER = 30;
int NUMber = 40;
System.out.println(number + " " + Number + " " + NUMBER + " " + NUMber);
// 10 20 30 40 -- all four are distinct variables
}
}
number, Number, NUMBER, and NUMber are four separate variables in the eyes of the compiler. Relying on case alone to distinguish names makes code confusing — prefer clearly different names instead.No Length Limit (But Keep It Reasonable)
Java places no hard limit on how long an identifier can be, but extremely long names hurt readability. Aim for names that are descriptive without being excessive.
int abcdefghijklmnopqrst = 7; // valid, but not a good name
int studentCount = 7; // valid and readable
Reserved Keywords Are Off-Limits
You cannot use one of Java's reserved keywords (class, if, else,
for, public, etc.) as an identifier — the compiler recognizes these words as
part of the language syntax, not as names.
int else = 25; // invalid - "else" is a reserved keyword
Predefined Class/Interface Names: Legal, But Bad Practice
Java does allow you to use the name of a predefined class or interface — like String
or Runnable — as a variable name. It compiles and runs fine, because Java can tell from
context whether you mean the type or the local variable. But it's a poor choice: it shadows the real class name
in that scope and makes the code harder to read for anyone (including future you) skimming it.
public class Test {
public static void main(String[] args) {
int String = 888; // legal, but "String" now means an int here
System.out.println(String); // prints 888
int Runnable = 42; // legal, but shadows the Runnable interface
System.out.println(Runnable); // prints 42
}
}
String or Runnable reduces readability and creates confusion for anyone who expects that name to refer to the actual type.camelCase for variables and methods, PascalCase for classes — and avoid shadowing built-in type names. Next up: Conditional Statements.