skip to content

Literals

Literal syntax across bases and types: hex, octal and binary integers, the L, f and d suffixes, escape and Unicode sequences, and underscores as digit separators. The leading-zero octal literal is the trap interviewers enjoy.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

6

What is a literal in Java, and what are the main categories of literals the language supports?

level: juniorimportance: must knowfreq 55%

answer

  1. Fixed value written directly in source
  2. Integer / float / boolean / char / string / null
  3. Default types: int, double, char, String
  4. Prefixes 0x 0b 0; suffixes L f d
  5. Resolved at compile time

basics

~10 s

A literal is a fixed value written directly in source code, like 42, 3.14, 'A', "hello", true, or null. Java has integer, floating-point, character, string, boolean, and the null literal.

solid answer

~40 s

A literal is a notation for a constant value written directly in the source code rather than computed at runtime. Java groups literals into a few kinds: integer literals (e.g. 42, 0x2A, 0b101, 052), floating-point literals (3.14, 1.5e3, 2.0f), the boolean literals true and false, character literals in single quotes ('A', '\n'), string literals in double quotes ("hi"), and the null literal. Each literal has a type the compiler infers: integer literals default to int, floating-point to double, character to char, string to String. Suffixes (L, f, d) and prefixes (0x, 0b, 0) change the value or type. Literals are evaluated at compile time and small ones may be interned or folded into constants.

go deeper

for a junior

List the literal kinds and give one example each; know that 42 is an int and 3.14 a double, that single quotes are chars and double quotes are strings, and that null is a literal.

for a middle

Explain default types and how suffixes/prefixes change type or base, and name the compile-time evaluation aspect.

for a senior

Tie categories to type-inference rules and compile-time constant folding; explain why a too-large literal fails to compile and how that shapes API/constant design.

for a principal

Discuss literals in terms of the JLS lexical grammar and compile-time constants (JLS §15.28), interning of String literals, and the downstream effects on readability and bug-prevention conventions in a codebase.

## What a literal is In programming, **source code** is the text a developer writes. Most values in a program are produced by *computation* (e.g. `a + b`), but sometimes you need to write a concrete value directly into the text — the number `42`, the word `"hello"`, the truth value `true`. Such a directly-written constant value is called a **literal**. The compiler reads the characters, figures out which value and which **type** (the kind of data: a whole number, a decimal number, a character, etc.) they represent, and bakes that value into the compiled program. Literals are therefore resolved at **compile time** (when the source is translated to bytecode), not at run time. ## The categories in Java The Java Language Specification defines these literal kinds: 1. **Integer literals** — whole numbers. Written in four bases: - **Decimal** (base 10): `42`, `0`, `1000`. - **Hexadecimal** (base 16): prefix `0x` or `0X`, digits 0-9 and a-f, e.g. `0x2A` is 42. - **Octal** (base 8): a leading `0`, digits 0-7, e.g. `052` is 42. (Note: `010` is 8, not 10 — a common trap.) - **Binary** (base 2): prefix `0b` or `0B`, e.g. `0b101010` is 42 (added in Java 7). By default an integer literal is of type `int` (32-bit). Append `L` (or `l`) to make it a `long` (64-bit): `42L`. 2. **Floating-point literals** — numbers with a fractional part or an exponent: `3.14`, `1.0`, `1.5e3` (scientific notation meaning 1.5 x 10^3 = 1500). By default these are `double` (64-bit). Append `f`/`F` for `float` (32-bit) or `d`/`D` to be explicit about `double`. 3. **Boolean literals** — exactly two: `true` and `false`. 4. **Character literals** — a single character in single quotes: `'A'`, `'7'`, `' '`. Special characters use **escape sequences** that begin with a backslash: `'\n'` (newline), `'\t'` (tab), `'\''` (single quote), `'\\'` (backslash). You can also write a **Unicode escape** `'A'` (the character whose code is 0x41 = 'A'). A `char` holds a 16-bit Unicode code unit. 5. **String literals** — zero or more characters in double quotes: `"hello"`, `""`. They produce a `String` object. The same escape sequences apply. 6. **The null literal** — `null`, the single value of the null type, assignable to any reference (object) variable to mean "no object". ## Why the categories matter The category determines the literal's **default type**, which affects what you can assign it to and how arithmetic behaves. For example `int x = 3000000000;` fails to compile because the literal is too big for `int`; you must write `long x = 3000000000L;`. Knowing the prefixes, suffixes, and escapes lets you write the exact value and type you intend. ## A note on what is NOT a literal Named constants (`Math.PI`), enum values, and expressions are not literals — they are references or computations. Even though `final int X = 5;` looks constant, `X` is an identifier; only the `5` is the literal.

  • What is the default type of the literal 100 and of 1.5?
    100 is an int (32-bit signed); 1.5 is a double (64-bit floating point). To change them you add suffixes: 100L is a long, 1.5f is a float.
  • Is 'A' the same as "A"?
    No. 'A' is a char literal (a single 16-bit Unicode code unit), while "A" is a String literal (a String object of length 1). They have different types and behave differently in expressions, e.g. 'A' + 1 is 66 but "A" + 1 is "A1".

saying these in an interview costs you the question

  • Thinking final constants or Math.PI are 'literals' — they are identifiers, only the raw written value is the literal
  • Forgetting null is a literal
  • Assuming integer literals default to long (they default to int)
  • Confusing a char literal 'A' (single quotes) with a one-char String "A" (double quotes)

context

open as a page

What do the L, f, and d suffixes do on numeric literals, and why can the L suffix actually change program behavior?

level: middleimportance: must knowfreq 60%

basics

~20 s

L makes an integer literal a long, f makes a number a float, and d makes it a double. Without L, big literals stay int and can overflow during arithmetic; adding L forces 64-bit math.

open as a page

How do you write integer literals in hexadecimal, octal, and binary, and what common bug does the octal form cause?

level: middleimportance: should knowfreq 48%

basics

~10 s

Hex uses prefix 0x (0x1F), binary uses 0b (0b1010), octal uses a leading zero (017). The trap: a leading zero makes the number octal, so 010 is 8, not 10.

open as a page

What are string literals and the string constant pool, and how do underscores in numeric literals work? When can you NOT use an underscore?

level: middleimportance: should knowfreq 42%

basics

~20 s

A string literal is text in double quotes; identical literals are shared (interned) in a string pool, so "hi" == "hi" is true. Underscores group digits in numbers for readability, like 1_000_000, but can't sit at the start, end, or next to a dot or base prefix.

open as a page

Explain character literals, escape sequences, and Unicode escapes (\uXXXX) in Java. What is unusual about how \uXXXX is processed?

level: seniorimportance: should knowfreq 38%

basics

~20 s

A char literal is one character in single quotes, like 'A'. Escapes like '\n' or '\t' write special characters. 'A' is a Unicode escape for 'A'. The surprise: \uXXXX is processed very early, before the code is even tokenized.

open as a page

How do floating-point literals and scientific notation work in Java, including special values and the difference between float and double precision?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Floating-point literals have a decimal point or an exponent: 3.14, 1.5e3 (means 1500), .5, 2f. Without a suffix they are double (64-bit, more precise); with f they are float (32-bit, less precise). There is no literal for infinity or NaN — those come from constants.

open as a page