Lesson · Java Language Fundamentals
Literals: writing values the compiler understands
11 min readLiterals
Every way to write a value in source: four number bases, underscores, suffixes, character escapes, the unicode pre-pass that trips everybody once, and text blocks for multi-line text.
Numbers: four bases and some sugar
A whole-number literal is an int unless the value forces otherwise. It may be written in decimal, hexadecimal (0x), binary (0b) or octal (a leading 0, which is a legacy form nobody should choose on purpose). Underscores may be placed between digits for readability, and the compiler ignores them entirely: 1_000_000 and 1000000 are the same value.
Suffixes decide the type when the default does not fit. L marks a long literal and is mandatory above the int range; without it, 9_000_000_000 is a compile error, since the digits claim a value the default type cannot hold. f and F mark a float, because a decimal literal with a dot is a double by default: float f = 1.5; fails, 1.5f compiles. Scientific notation (1e3) produces a double.
Literal lab
pick a literalHexadecimal: base 16, and the f's are 15s. The underscores are decoration for humans; the compiler ignores them entirely.
Chips show the source text; the card shows what the compiler stores.
Characters, escapes, and the unicode pre-pass
A char literal sits in single quotes and holds exactly one UTF-16 code unit. Escape sequences cover the unprintable: \n, \t, \\, \', \", \r, and the unicode form \uXXXX. Everyone meets these in the first week and mostly stops thinking about them, except for one rule.
The unicode escape is processed before the parser sees anything, as a text substitution over the whole file. In comments, in strings, everywhere. Two consequences follow. A \u sequence inside a comment is still decoded, so a malformed one (\uZZZZ) breaks compilation with a message about the comment. And '\u0027' is not an escaped quote: the pre-pass turns it into a literal quote character, producing three quotes in a row and a syntax error. When \u appears inside source, the compiler has already been there first.
Strings and text blocks
String literals use double quotes and the same escape set. Multi-line text no longer needs concatenation: a text block opens with """ followed by a line terminator, and preserves everything until the closing delimiter. Incidental indentation (the columns shared by the content and the closing delimiter) is stripped automatically, which keeps JSON and SQL snippets readable in source without mangling them in the value.
Two small block conveniences: a trailing backslash suppresses the newline for line continuations, and \s represents a space that survives trailing-whitespace trimming by editors and tools. Inside a block, a lone double quote needs no escape; quotes are only special in pairs.
The traps worth a memory slot
A leading zero means octal, so 017 is 15, not seventeen. A decimal literal without a suffix is a double, so float f = 1.5; is a type error while 1.5f is not. A whole-number literal above the int range without L does not compile. A char in double quotes or a string in single quotes is always wrong: the quotes pick the type. And one day a stray \u will land in a comment through copy-paste, and the error message will insist the file has an illegal unicode escape. That one is not your logic; it is the pre-pass doing exactly what the spec says.
Unicode escapes run before parsing
\uXXXX sequences are substituted over the entire file before the parser starts, including inside comments and strings. An invalid sequence in a comment fails compilation, and '\u0027' inside a char literal is just a quote character, so the literal breaks. The compiler is not confused; it obeyed the pre-pass.
Interview angles
What are the default types of numeric literals?+
A whole-number literal is an int (long needs a trailing L, mandatory beyond the int range). A decimal literal is a double (float needs f). Scientific notation is a double. The suffix overrides the default; the default never bends to the variable on the left.
Why is 017 not seventeen?+
A leading zero selects octal notation, so 017 is 1×8 + 7 = 15. It is a legacy C form kept for compatibility. Avoid leading zeros on decimal literals; the compiler will not warn, and readers will misread values exactly once before it sticks.
How can a comment break compilation?+
Unicode escapes are decoded across the whole file before parsing, comments included. A sequence like \uZZZZ in a comment is an illegal unicode escape, and the file fails to compile even though the comment would otherwise be ignored entirely.
When is a text block better than concatenation?+
Whenever literal text spans lines: JSON, SQL, HTML, test fixtures. Indentation is stripped relative to the closing delimiter, quotes need no escaping, and the source keeps the shape of the value. Use \s for meaningful trailing spaces and a trailing backslash to join lines.