Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

util.UUID: practical reference

util.UUID: practical reference: UUID represents an immutable 128-bit value; the class methods manipulate the Leach-Salz variant layout, although its constructor can create any UUID variant.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.util.UUID

UUID represents an immutable 128-bit value; the class methods manipulate the Leach-Salz variant layout, although its constructor can create any UUID variant.

Use randomUUID for locally generated version-4 identifiers, or nameUUIDFromBytes when identical bytes must deterministically produce a version-3 UUID.

Do not treat a random UUID as proof of identity or creation order; natural compareTo ordering is not chronological.

timestamp, clockSequence, and node work only for version-1 UUIDs and throw UnsupportedOperationException for other versions; fromString rejects malformed input with IllegalArgumentException.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        var value = java.util.UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
        System.out.println(value.version());
        System.out.println(value);
    }
}

Result

1
123e4567-e89b-12d3-a456-426614174000

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources