CKTIS / Java
Java integer literals: `int`, `long`, and `0L`
Learn when an integer literal is an `int` or a `long`, how to write large values, and why `0L` is useful for running totals and conversions to `Long`.
Version: Java SE 21
Syntax
IntegerLiteral ::= (DecimalNumeral | HexNumeral | OctalNumeral | BinaryNumeral) [IntegerTypeSuffix]
IntegerTypeSuffix ::= "L" | "l"
Examples: 0, 0L, 2_147_483_648L, 0x10L, 077L, 0b11LIn Java SE 21, 0L is a decimal literal for zero of primitive type long (64 bits), whereas 0 is an int (32 bits). The suffix L or l makes an integer literal a long; prefer L because lowercase l looks like the digit 1. Given overloads f(int) and f(long), f(0) selects the first and f(0L) selects the second.
Integer literals can be decimal (42), hexadecimal (0x2A or 0X2A), octal (with a leading zero, as in 052), or binary (0b101010 or 0B101010). Underscores separate digits, as in 2_147_483_648L or 0_52. They cannot go immediately after a hexadecimal or binary prefix, or after the last digit, even before L.
The destination type does not change the literal's type: long n = 2_147_483_648; fails to compile, but long n = 2_147_483_648L; works. The decimal boundary literals 2147483648 and 9223372036854775808L are allowed only as operands of unary minus -. In hexadecimal, octal, and binary, the type's top bit is the sign bit: 0xFFFF_FFFF is the int value -1, but 0xFFFF_FFFFL is positive 4294967295L. A numeral that needs more than 32 bits for int or 64 bits for long is a compile-time error.
Use long total = 0L; to explicitly start a long running total at zero. long total = 0; also works: the int widens to long. In total + 1, 1 widens before addition. But storing a result in long does not prevent overflow in an int calculation: long n = 2_000_000_000 + 2_000_000_000; gives -294967296. Add L to either operand to make that addition use long. Overflow in long arithmetic also does not throw an exception.
Long boxed = 0L; boxes the primitive value into java.lang.Long. Long boxed = 0; does not compile: assignment does not combine widening from int to long with boxing to Long. Both long n = boxed; and boxed + 1 unbox to long; if boxed is null, unboxing throws NullPointerException.
Examples
public class Main {
public static void main(String[] args) {
long total = 0L;
total += 2_147_483_648L;
total += 0x10 + 0b11 + 07;
Long boxed = total;
long next = boxed + 1;
System.out.println(total);
System.out.println(next);
}
}Result
2147483674 2147483675
`0x10`, `0b11`, and `07` add up to the `int` value `26`, which widens to `long` in `total += ...`. Assigning to `boxed` boxes the total, and `boxed + 1` unboxes it before adding.
Related lessons
- Java · Lesson 39 · Advanced · Version: 2
Sources
- Java Language Specification, Java SE 21 — §3.10.1 Integer Literals
- Java Language Specification, Java SE 21 — §4.2.2 Integer Operations
- Java Language Specification, Java SE 21 — §5.1.7 Boxing Conversion
- Java Language Specification, Java SE 21 — §5.1.8 Unboxing Conversion
- Java Language Specification, Java SE 21 — §5.2 Assignment Contexts
- Java Language Specification, Java SE 21 — §5.6 Numeric Contexts
- Java Language Specification, Java SE 21 — §15.12.2 Compile-Time Step 2: Determine Method Signature
- Java Language Specification, Java SE 21 — §15.18.2 Additive Operators (+ and -) for Numeric Types
- Long (Java SE 21 & JDK 21)
- The javac Command
- The java Command