Developer Documentation
Java
Updated September 19, 2026

Generate and Work with UUIDs in Java

Generate, parse, format, and validate UUIDs safely in Java.

UUIDs are 128-bit identifiers standardized by RFC 9562. UUIDv4 is random and broadly supported; newer UUID versions add other generation properties while keeping the same canonical textual shape.

Quick Start#

The following example uses the standard Java API or a widely used package for this task.

Quick start#

Java
1import java.util.UUID;23UUID id = UUID.randomUUID();4System.out.println(id);5System.out.println(id.version()); // 4

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Parse and inspect#

Java
1try {2    UUID parsed = UUID.fromString(input);3    System.out.println(parsed.version());4    System.out.println(parsed.variant());5} catch (IllegalArgumentException ex) {6    System.out.println("Invalid UUID");7}

Important Notes#

Use UUID values as objects internally and canonical strings at serialization boundaries. Check your chosen library/runtime separately if you need UUID versions beyond those directly exposed by the standard API.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#