Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Java’s `L` integer literal suffix

`0L` is zero of type `long`, while `0` is zero of type `int`. That difference matters for overload selection, arithmetic, and defaults that must be `Long`.

Version: Java SE 21

Syntax

IntegerLiteral:
    DecimalNumeral [IntegerTypeSuffix]
    HexNumeral [IntegerTypeSuffix]
    OctalNumeral [IntegerTypeSuffix]
    BinaryNumeral [IntegerTypeSuffix]

IntegerTypeSuffix: L | l

In Java SE 21, a valid integer literal has type int without a suffix and type long with the L integer literal suffix or its lowercase form l. Write the suffix immediately after the final digit of a decimal, hexadecimal, octal, or binary numeral. Prefer uppercase L: lowercase l is easy to mistake for 1. 0L and 0 have the same value but different types.

long timeout = 0; works because Java widens int to long; long timeout = 0L; starts with a long literal. The destination type cannot rescue an oversized literal: 2_147_483_648 in an assignment is a compile-time error, whereas 2_147_483_648L is valid. Without L, that decimal magnitude is permitted only directly after unary minus (-2147483648); the analogous long exception is -9223372036854775808L.

With kind(int) and kind(long) available, kind(0) calls the int overload and kind(0L) calls the long overload. In 1_000_000 * 3_000, multiplication runs in int and overflows even if the result is assigned to a long. In 1_000_000L * 3_000, Java promotes the other operand to long before multiplying. A long can still overflow.

For Map<String, Long>, the second argument to getOrDefault must be compatible with Long: 0L boxes to Long, while 0 would box to Integer. Java does not combine widening from int to long with boxing to Long for this call. The method uses the default only when the key has no mapping.

Examples

import java.util.Map;

public class IntegerLiteralSuffixDemo {
    static String kind(int value) { return "int"; }
    static String kind(long value) { return "long"; }

    public static void main(String[] args) {
        long timeout = 0L;
        Map<String, Long> timeouts = Map.of("read", 2_147_483_648L);
        Long missing = timeouts.getOrDefault("write", 0L);

        System.out.println("timeout=" + timeout);
        System.out.println("kind(0)=" + kind(0));
        System.out.println("kind(0L)=" + kind(0L));
        System.out.println("read=" + timeouts.getOrDefault("read", 0L));
        System.out.println("write=" + missing);
        System.out.println("int product=" + (1_000_000 * 3_000));
        System.out.println("long product=" + (1_000_000L * 3_000));
    }
}

Result

timeout=0
kind(0)=int
kind(0L)=long
read=2147483648
write=0
int product=-1294967296
long product=3000000000

The map has no `write` key, so the method returns `0`. The last two lines contrast the overflowed `int` product with the correct `long` product.

Related lessons

  • Java · Lesson 30 · Advanced · Version: 2
  • Java · Lesson 31 · Advanced · Version: 2
  • Java · Lesson 43 · Advanced · Version: 2

Sources