A Universally Unique Identifier (UUID) is a 128-bit value used to uniquely identify data across systems.
A UUID is 36 characters long (32 hex digits + 4 hyphens), e.g.
550e8400-e29b-41d4-a716-446655440000
.
Common types include v1 (time-based), v3/v5 (namespace + hash), and v4 (random, most used today).
They avoid ID collisions across distributed systems, unlike simple auto-increment IDs in databases.
UUIDs are unique but not cryptographically secure. Use them as identifiers, not as secrets or tokens.
UUIDs are widely used in databases, API keys, session identifiers, distributed systems, and file names.
UUID v4 is generated randomly, making it highly unpredictable. UUID v3 and v5 are deterministic — the same input will always produce the same UUID.
Use v4 for randomness, v3/v5 for consistency (like generating the same ID from a namespace).
import java.util.UUID;
public class UuidExample {
public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
System.out.println("Generated UUID: " + uuid);
}
}
Java’s UUID
class makes it simple to generate v4 UUIDs directly.
UUIDs are larger than integers and may impact indexing in databases. Alternatives include ULIDs (lexicographically sortable) or Snowflake IDs (Twitter).
Choose based on your need for uniqueness, ordering, or efficiency.