Basic Syntax
Keywords and Identifiers
Dissect Java's reserved keywords, soft keywords, identifier naming rules, and standard coding conventions.
Interview: Tests identifier validity rules, dynamic keyword categories (like var/yield), and reserved vs unused terms.
In Java, source code is parsed into tokens. Two of the most fundamental token classes are Keywords (words reserved by the language compiler for syntax) and Identifiers (names defined by programmers to represent classes, variables, methods, and packages).
Core Idea
Reserved keywords have predefined meanings that cannot be changed. Identifiers must follow strict compiler rules to distinguish variables from literal numbers.
Why It Matters
Java's expansion has introduced "soft/restricted keywords" (like var and yield) that change compiler behavior depending on context, affecting backwards compatibility.
Interview Lens
Expect questions targeting invalid variable naming edge cases, reserved keywords that are unused (like goto), and identifier Unicode boundaries.
Java Keywords
Java has over 50 keywords. These are divided into two main categories:
- Hard Reserved Keywords: Always reserved by the compiler. You cannot declare a variable, class, or method named after these. Examples include
class,public,static,void,if,else,return,int, andnew.- Unused Reserved Keywords:
constandgotoare reserved keywords in Java, even though they serve no syntax function. This prevents programmers from writing buggy code resembling other procedural languages, allowing the compiler to yield clear, targeted errors instead.
- Unused Reserved Keywords:
- Soft/Restricted Keywords: Words introduced in modern Java versions that act as keywords only in specific syntax contexts. To maintain backwards compatibility with code written in older Java versions, these words can still be used as variable names elsewhere. Examples:
var: Treated as local variable type inference keyword (Java 10+), but you can still writeint var = 10;(legal, though discouraged).yield: Used to return values from switch expressions (Java 14+).record,sealed,non-sealed,permits: Structural keywords introduced in Java 16/17 for data layout encapsulation.
Identifier Rules and Conventions
An identifier is a sequence of characters of any length that represents a named element.
Strict Compiler Rules:
- Must begin with a letter, currency character (
$), or connecting character (_). It cannot begin with a digit. - Subsequent characters can be letters, digits,
$, or_. - Cannot be a hard reserved keyword. (Note:
true,false, andnullare literals, not keywords, but they are also strictly forbidden as identifiers). - Identifiers are case-sensitive.
myVariableandmyvariablerepresent two entirely separate names in compiled symbol tables.
Naming Conventions (Best Practices):
- Classes and Interfaces: Use PascalCase (e.g.,
class InvoiceManager). - Methods and Variables: Use camelCase (e.g.,
void calculateTotal(),int itemCount). - Constants: Use UPPER_SNAKE_CASE (e.g.,
final int MAX_RETRIES = 3;). - Currency Character ($): Avoid using
$in variable names. The Java compiler uses this character internally to generate naming structures for inner classes (e.g.,Outer$Inner.class).
Common Pitfalls
- Starting with a number: Writing
int 1stValue = 10;will cause a compilation error. Useint value1storint firstValueinstead. - Using Reserved words: Forgetting that literals like
null,true, andfalseare forbidden variable identifiers. - Abusing underscores: Java 9+ treats a single underscore (
_) as a keyword representing an unnamed variable. Writingint _ = 5;was legal in Java 8 but is a compiler error in modern Java versions.
Interview-Relevant Information
Q1: What is the difference between reserved keywords and literals?
Answer: Keywords (like class or public) define language syntax commands. Literals (like true, false, and null) represent static, immutable data values. However, both are strictly prohibited as identifier names.
Q2: Is a single underscore (_) a valid identifier in modern Java?
Answer: No. Since Java 9, a single underscore _ is reserved as a keyword to denote unnamed variables or pattern parameters. This prevents developers from using it as a standard identifier name.
Quick Checklist
Can you identify which characters are legal to start a variable, explain the role of soft keywords, and explain why goto is reserved? If yes, you understand Java's naming rules.
Use Cases
Writing backwards-compatible code when integrating older Java packages.
Structuring constants using static final conventions to protect immutable configuration tokens.
Common Mistakes
Attempting to name variables starting with numbers (e.g., 2factorAuth).
Using single underscores (_) as temporary loop variable identifiers, which fails in modern Java compilers.