What is a literal in Java, and what are the main categories of literals the language supports?
answer
- Fixed value written directly in source
- Integer / float / boolean / char / string / null
- Default types: int, double, char, String
- Prefixes 0x 0b 0; suffixes L f d
- Resolved at compile time
basics
~10 sA 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 sA 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
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.
Explain default types and how suffixes/prefixes change type or base, and name the compile-time evaluation aspect.
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.
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)