CKTIS / Java
Java integer literals: the L suffix
Write `10000000001L` to make a `long` literal. Without the suffix, the value is too large for `int`, even if the destination variable is declared `long`.
Version: Java SE 21
Syntax
IntegerLiteral ::= (DecimalNumeral | HexNumeral | OctalNumeral | BinaryNumeral) [IntegerTypeSuffix]
IntegerTypeSuffix ::= "L" | "l"
long value = 10000000001L;In Java SE 21, an integer literal writes a value directly in source code. The suffix L or l makes it a long (signed 64-bit); without a suffix, its type is int (signed 32-bit). Prefer uppercase L because lowercase l can look like the digit 1.
10000000001L is a decimal long literal. Its value exceeds 2147483647, the largest int value, but fits in long. The declaration long value = 10000000001; fails at compile time: the variable's type does not change the literal's type. By contrast, long value = 42; works because assignment implicitly widens the valid int literal to long without changing its value.
Integer literals can be decimal, hexadecimal (0x), octal (leading 0), or binary (0b). Underscores between digits improve readability: 10_000_000_001L means the same as 10000000001L. An underscore cannot appear immediately before the suffix.
In decimal notation, the boundary literals 2147483648 and 9223372036854775808L are allowed only as direct operands of unary minus -. The expressions -2147483648 and -9223372036854775808L produce the smallest int and long values, respectively. The minus sign is an operator, not part of the literal.
Examples
public class IntegerLiteralDemo {
public static void main(String[] args) {
long large = 10000000001L;
long widened = 42;
System.out.println(large);
System.out.println(widened);
}
}Result
10000000001 42
The expected output is `10000000001`, then `42` on the next line. The `L` is part of the source notation, not the printed value. Remove it from the large literal to check that the error occurs at compile time, not at run time.
Related lessons
- Java · Lesson 46 · Intermediate · Version: 2
Sources
- Java Language Specification, Java SE 21 — §3.10.1 Integer Literals
- Java Language Specification, Java SE 21 — §4.2.1 Integral Types and Values
- Java Language Specification, Java SE 21 — §5.1.2 Widening Primitive Conversion
- Java Language Specification, Java SE 21 — §5.2 Assignment Contexts
- Java SE 21 API — PrintStream.println(long)
- JDK 21 Tool Specifications — The java Command