SSOGEN / Learn / Java UUID: randomUUID, version 7 and what the JDK lacks
Learn
Java UUID: randomUUID, version 7 and what the JDK lacks
To make a java uuid, call UUID.randomUUID(): it returns a random version 4 value with 122 random bits. JDK 26 adds version 7 for database keys, and no UUID is a secret.

UUID id = UUID.randomUUID();System.out.println(id); // 7d16bba9-efa6-4c68-afca-0e93abed7f7aSystem.out.println(id.version()); // 4randomUUID() is the call most programs need. The JDK documents it as a type 4 UUID from a cryptographically strong pseudo-random number generator. Every output below was produced on OpenJDK 21.0.12.1 on 2026-11-17.
Generate, parse and compare: what the run shows
Parsing is lenient and compareTo is signed, so test both before you rely on them. This is what each call returned on JDK 21.0.12.1.
| Call | Result | What to know |
|---|---|---|
UUID.randomUUID() |
7d16bba9-efa6-4c68-afca-0e93abed7f7a |
Version 4, variant 2, 36 characters as text |
UUID.fromString("123E4567-E89B-42D3-A456-426614174000") |
equals the lowercase form: true |
Case does not matter |
UUID.fromString("1-1-1-1-1") |
00000001-0001-0001-0001-000000000001 |
Accepted; short groups are padded |
UUID.fromString("123e4567e89b42d3a456426614174000") |
IllegalArgumentException: Invalid UUID string |
The hyphens are required |
lo.compareTo(hi) for 00000000-0000-0000-0000-000000000001 and ffffffff-ffff-ffff-ffff-ffffffffffff |
1 |
In this run lo compares greater, the reverse of text order |
If you need strict validation, check the 8-4-4-4-12 pattern with a regular expression, then check version() and variant(). For sorting, compare the text or the bytes, not compareTo. This behaviour was observed on JDK 21.0.12.1 only; the UUID API page does not describe it.
nameUUIDFromBytes is MD5 without a namespace
UUID.nameUUIDFromBytes(bytes) returns a version 3 UUID, but it hashes only the bytes you pass. RFC 9562 section 5.3 hashes a namespace identifier first, so the two results differ.
JDK call: nameUUIDFromBytes("example.com".getBytes(UTF_8)). It returned 5ababd60-3b22-3803-82dd-8d83498e5172.
RFC 9562 version 3: the DNS namespace 6ba7b810-9dad-11d1-80b4-00c04fd430c8 and the name example.com. The result is 9073926b-929f-31c2-abc9-fad77ae3e8eb, which Python’s uuid.uuid3 also returns.
Use the JDK call when you only need a repeatable ID from one input. Build the namespace form yourself when another system must reproduce the value.
Version 7 and the JDK
Version 7 puts a 48-bit Unix millisecond timestamp first, then 74 random bits (RFC 9562 section 5.7). Its text form sorts in creation order. Read on 2026-11-17, the JDK API pages show where it exists.
| JDK | Version 7 factory |
|---|---|
| 21 | None; the javap listing has randomUUID, nameUUIDFromBytes and fromString |
| 25 | None |
| 26 and 27 | UUID.ofEpochMillis(long), marked “Since: 26” |
On JDK 26 and later, call UUID.ofEpochMillis(System.currentTimeMillis()). The API note says callers who need monotonic values must pass a monotonic timestamp. On JDK 21 and 25, this method follows the same layout:
static final SecureRandom RNG = new SecureRandom();
static UUID v7(long millis) { // 48-bit ms + 74 random bits long msb = (millis << 16) | 0x7000L | (RNG.nextLong() & 0x0FFFL); long lsb = (RNG.nextLong() & 0x3FFFFFFFFFFFFFFFL) | 0x8000000000000000L; return new UUID(msb, lsb);}Run with 1700000000000L, it gave 018bcfe5-6800-76aa-a622-6bf23339ba9a: version 7, variant 2, and a timestamp that reads back as 2023-11-14T22:13:20Z. The part after 7 is random and differs on every run.
The ordering is real but bounded. In one run, 1,000 values for 1,000 consecutive milliseconds sorted ascending as text every time; 1,000 version 4 values were ascending in 481 of 999 neighbouring pairs, about half. Within a single millisecond, 100 of 200 pairs were out of order. For strict order inside one millisecond, use a library or the counter methods in RFC 9562 section 6.2.
UUIDs as database keys
Store a UUID as 16 bytes, not as text. RFC 9562 section 6.13 says text needs 288 bits for a 128-bit value and that databases should store the binary value where feasible. The same section advises against name-based UUIDs as primary keys.
Time-ordered values also keep new rows near each other in the index. Section 6.11 states that the difference against random inserts “can be one order of magnitude or more”. That is the RFC’s wording, not a measurement made here.
A UUID is not a token
For an API secret or a session value, generate one with a random string generator or an API key generator, which report entropy in bits. The same rule applies to session identifiers: see JSESSIONID for what a servlet container issues. For stored passwords, see Java password hashing.
Questions people ask
Is UUID randomly generated?
Version 4 is; others are not. randomUUID() fills 122 bits from a cryptographically strong generator. Versions 3 and 5 hash a name, so the same name gives the same UUID. Version 7 combines a millisecond timestamp with 74 random bits.
Is a GUID 128 bits?
Yes. RFC 9562 says a UUID is 128 bits long and is also known as a GUID. In Java that is two long values, getMostSignificantBits() and getLeastSignificantBits(), and 16 bytes when stored in binary.
What is a valid UUID?
Five hexadecimal groups of 8-4-4-4-12 digits, in either case, with valid version and variant bits. UUID.fromString accepts less, as 1-1-1-1-1 shows. For strict checks, match the pattern with a regular expression, then test version() and variant().
Is UUID 36 characters?
As text, yes. The form is 32 hexadecimal digits plus 4 hyphens, which makes 36 characters. As binary it is 16 bytes. Storing the 36-character text form costs more space than storing the 16 bytes.
Sources
Sources, all read 2026-11-17: JDK 21 UUID API, JDK 26 UUID API, JDK 27 UUID API and RFC 9562, sections 5.3, 5.7, 6.2, 6.11, 6.13 and 8.
Updated