skip to content

Are `true`, `false`, and `null` keywords in Java? Where do they fit among reserved words?

level: middleimportance: nice to knowfreq 35%

answer

  1. Reserved literals, NOT keywords (per JLS)
  2. true/false = boolean literals; null = null literal
  3. Still off-limits as identifiers
  4. Separate bucket from the ~50 keywords
  5. int null = 0; won't compile

basics

~10 s

Technically they are reserved literals, not keywords — true/false are boolean literals and null is the null literal. But like keywords, you cannot use them as names.

solid answer

~40 s

Strictly per the Java Language Specification, `true`, `false`, and `null` are not classified as keywords; they are reserved literals — `true` and `false` are the two boolean literal values, and `null` is the null literal (the absence of an object reference). The keyword list and the literal list are separate categories in the spec. In everyday practice, though, the distinction rarely matters because all three are equally reserved: you cannot use any of them as an identifier. `int null = 0;` is a compile error just like `int class = 0;`. So the precise answer is: they're reserved words but in the 'literal' bucket, not the 'keyword' bucket — a nuance worth knowing for correctness, even if both buckets are off-limits as names.

code

java · 7 lines
java
boolean ready = true;   // 'true' is a boolean literal
String name = null;     // 'null' is the null literal

// All three are reserved — these are compile errors:
// int true = 1;
// int null = 0;
// boolean false = true;

go deeper

for a junior

Knows true/false/null are special words you can't use as names and what each value means.

for a middle

Can state the precise nuance that they are reserved literals rather than keywords, yet still reserved against identifier use.

for a senior

Explains the JLS categorization (keywords vs. literals), why values sit in a separate bucket, and the null-type detail.

for a principal

Connects literal categorization to grammar design and type-system rules (the null type's assignment compatibility), and why the spec keeps the categories distinct.

## Two separate buckets in the spec The **Java Language Specification (JLS)** — the official rulebook for the language — divides the words you cannot use as names into separate categories: 1. **Keywords** — words that drive grammar/semantics: `class`, `if`, `int`, `public`, etc. (~50 of them). 2. **Reserved literals** — specific *values* written directly in code: the **boolean literals** `true` and `false`, and the **null literal** `null`. A **literal** is a value typed directly into source code: `42` is an int literal, `"hi"` is a string literal, `true` is a boolean literal. Unlike numeric or string literals, the words `true`, `false`, and `null` are **reserved** so they always mean those exact values and can never be redefined. ## What each one means - **`true` / `false`** — the only two values of the `boolean` type. `boolean ready = true;` stores the literal value. - **`null`** — represents 'no object'; a reference variable that points to no object holds `null`. `String s = null;` means `s` refers to nothing. ## Why the distinction exists Grammatically, keywords appear in *syntactic* positions (declaring, branching, modifying), whereas `true`/`false`/`null` appear wherever a *value* is expected — they behave like the literals `42` or `'x'`, not like `if` or `class`. The JLS therefore files them under literals. But it still **reserves** them, because allowing `int true = 5;` would make `boolean b = true;` ambiguous. ## The practical bottom line For day-to-day coding the categories collapse: **all three are off-limits as identifiers**, exactly like real keywords. `int null = 0;` won't compile. The reason this is worth knowing is interview precision: if asked 'is `null` a keyword?', the technically correct answer is 'no — it's a reserved literal (the null literal), though it's reserved just like a keyword.' Demonstrating that you know the spec's categorization signals careful, accurate knowledge rather than a memorized list.

  • If they aren't keywords, can I use them as variable names?
    No. They are reserved literals, and reservation forbids identifier use just as it does for keywords. `int null = 0;` is a compile error.
  • What type is null?
    null is the null literal of the special 'null type', which is assignment-compatible with any reference type. It is never assignable to a primitive.

Think of a dictionary with two locked sections: one for grammar words (keywords) and one for fixed values (literals). Both are locked, but they're filed on different shelves.

saying these in an interview costs you the question

  • Flatly stating true/false/null are keywords without the literal nuance
  • Thinking null can be assigned to a primitive like int (it cannot — only to reference types)
  • Believing you can shadow null/true/false as identifiers because they 'aren't keywords'

context