UUID Name Based Version

Derive a version 3 UUID with nameUUIDFromBytes, check its version and variant, and contrast it with a random version 4 UUID.

JavaData Integrity

Code

snippet
import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class Main {
    public static void main(String[] args) {
        byte[] name = "devexamples.com/examples/uuid".getBytes(StandardCharsets.UTF_8);

        // nameUUIDFromBytes derives a version 3 UUID from the bytes
        UUID key = UUID.nameUUIDFromBytes(name);
        System.out.println("uuid: " + key);
        System.out.println("version: " + key.version());
        System.out.println("variant: " + key.variant());

        // the same bytes always produce the same identifier
        UUID again = UUID.nameUUIDFromBytes(name);
        System.out.println("stable: " + key.equals(again));

        // different input bytes produce a different identifier
        UUID other = UUID.nameUUIDFromBytes(
                "devexamples.com/examples/other".getBytes(StandardCharsets.UTF_8));
        System.out.println("different: " + !key.equals(other));

        // randomUUID uses version 4 instead of a name hash
        UUID random = UUID.randomUUID();
        System.out.println("random version: " + random.version());
        System.out.println("random differs: " + !random.equals(key));
    }
}

How the snippet works

The moving parts are the class Main and the function main.

What to watch for

  • Nothing arrives from a registry; the language ships every API used.

Using it

The primary signature is public static void main(String[] args)

Output lines are prefixed (uuid:, version:, variant:), making each step’s effect easy to spot.

Key statements

byte[] name = "devexamples.com/examples/uuid".getBytes(StandardCharsets.UTF_8);
UUID again = UUID.nameUUIDFromBytes(name);
UUID key = UUID.nameUUIDFromBytes(name);

Interactive Tools for This Topic

FAQ

What does UUID Name Based Version print when run?
Invoke the file as-is — the `uuid:`, `version:`, `variant:` prefixes identify which step printed each line.