Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Hexadecimal integer literal

A hexadecimal integer literal expresses a value in base 16 using the prefix 0x or 0X. The suffix L selects long; without a suffix, the literal has type int.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

HexIntegerLiteral = ("0x" | "0X") HexDigits ["L" | "l"]; HexDigits = HexDigit { ["_" {"_"}] HexDigit }; HexDigit = "0"…"9" | "a"…"f" | "A"…"F"

A hexadecimal integer literal starts with 0x or 0X. Digits are 0–9 and A–F (lowercase also works); 0xFF means 255. Underscores, including repeated ones, may separate digits, but cannot immediately follow the prefix or precede the suffix.

Without a suffix, the literal has type int (32 bits); L or l selects long (64 bits). Prefer L. Exceeding the width causes a compilation error. 0xFFFF_FFFF means -1, whereas 0xFFFF_FFFFL means 4294967295. Assignment to long alone does not change the literal's type.

Hexadecimal notation is not text. Integer.toHexString(value) produces lowercase text without a prefix, interpreting its argument as unsigned. For 255, it returns "ff".

Examples

public class Main {
    public static void main(String[] args) {
        int value = 0xFF;
        int signed = 0xFFFF_FFFF;
        long positive = 0xFFFF_FFFFL;

        System.out.println(value);
        System.out.println(signed);
        System.out.println(positive);
        System.out.println(Integer.toHexString(value));
    }
}

Result

255
-1
4294967295
ff

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources