Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Text, Formatting and Regex

The UUID class

Reference for java.util.UUID: random version 4 IDs, name-based version 3 IDs, parsing and formatting, comparison, bits, and when UUIDs are a poor database key.

A java.util.UUID is a 128-bit identifier written as 32 hexadecimal digits in five groups, for example 123e4567-e89b-42d3-a456-426614174000. Its purpose is to create identifiers that are unique without a central authority: two machines can generate IDs independently and still be practically certain not to collide.

Java can generate two kinds. UUID.randomUUID() creates a version 4 UUID from a cryptographically strong random generator (122 random bits). UUID.nameUUIDFromBytes(byte[]) creates a version 3 UUID, the MD5 hash of the bytes, so the same input always yields the same UUID. Time-ordered versions 1, 6 and 7 can be read but not generated by the standard library.

Java
UUID id = UUID.randomUUID();
UUID same = UUID.fromString(id.toString());

Try it

Java Parsing, formatting and comparing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Name-based UUIDs and use in collections

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public final class UUID implements Serializable, Comparable<UUID>
public UUID(long mostSigBits, long leastSigBits)
Member Description
static UUID randomUUID() New random (version 4) UUID using SecureRandom.
static UUID nameUUIDFromBytes(byte[]) Version 3 UUID from the MD5 hash of the bytes.
static UUID fromString(String) Parses the standard 8-4-4-4-12 form (case-insensitive).
toString() Lowercase form with hyphens.
version(), variant() Version number (1 to 8) and variant (2 for RFC 4122 / RFC 9562 IDs).
getMostSignificantBits(), getLeastSignificantBits() The two long halves.
timestamp(), clockSequence(), node() Only for version 1; otherwise UnsupportedOperationException.
compareTo(UUID), equals, hashCode Ordering and equality by the signed bit halves.

Throws: IllegalArgumentException from fromString for malformed text.

Usage notes

  • Collisions among random UUIDs are astronomically unlikely (about 5.3 x 10^36 possible values), so treat them as unique. A UUID is an identifier, though, not a secret: do not use it as a password or as the only protection of a resource.
  • Database keys. Fully random UUIDs scatter inserts across a B-tree index and use 16 bytes (or 36 characters as text). Store them in a native UUID or BINARY(16) column; for write-heavy tables consider time-ordered IDs (version 7) generated by a library or the database.
  • Version 3 uses MD5; it is fine for stable IDs from names but there is no namespace parameter in the Java API, so prefix the name yourself. Version 5 (SHA-1) is not offered.
  • Sorting caveat. compareTo compares the halves as signed longs, so it does not match the lexicographic order of the text form.
  • Immutable and thread-safe. Equality uses value, so UUIDs work as Map keys and set elements.
  • Parsing is lenient about digits: fromString accepts short groups (for example 1-1-1-1-1 becomes 00000001-0001-0001-0001-000000000001); validate the format yourself if input is untrusted.
  • Generating for tests: never rely on randomUUID() output in assertions; inject the value or use new UUID(hi, lo).