DevToolsForYou

UUID Generator in Java — Code Examples

UUID Generator in JavaUse the online tool →

UUIDs (Universally Unique Identifiers) are 128-bit identifiers used as database primary keys, session tokens, and idempotency keys. UUID v4 (random) is the safe default; UUID v7 (time-ordered) is preferred for database primary keys.

Java's standard library includes java.util.UUID with randomUUID() for v4 and nameUUIDFromBytes() for v3.

Java
import java.util.UUID;

public class UUIDExample {
    public static void main(String[] args) {
        // UUID v4 — random
        UUID id = UUID.randomUUID();
        System.out.println(id);           // e.g. 110e8400-e29b-41d4-a716-446655440000
        System.out.println(id.toString()); // same
        System.out.println(id.toString().replace("-", "")); // without hyphens

        // UUID v3 — deterministic (MD5-based)
        UUID v3 = UUID.nameUUIDFromBytes("example".getBytes());
        System.out.println(v3);

        // Parse a UUID string
        UUID parsed = UUID.fromString("110e8400-e29b-41d4-a716-446655440000");
        System.out.println(parsed.version()); // 4
        System.out.println(parsed.variant()); // 2

        // UUID as two longs (for compact storage)
        long msb = id.getMostSignificantBits();
        long lsb = id.getLeastSignificantBits();
        UUID reconstructed = new UUID(msb, lsb);
    }
}
Notes & gotchas
  • UUID.randomUUID() uses SecureRandom — it is cryptographically secure.
  • Java's standard library only supports v3 and v4; use a library like java-uuid-generator for v1 and v7.
  • Storing UUIDs as two BIGINT columns (getMostSignificantBits / getLeastSignificantBits) is more efficient than a VARCHAR(36).
Try it in your browser

Need to uuid generator without writing code? The UUID Generator runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open UUID Generator
UUID Generator in other languages