How to Generate a UUID in Java
The standard way to generate a UUID in Java is UUID.randomUUID(), built into the JDK since Java 1.5 — no dependencies, no configuration required. It draws from a cryptographically secure random source to produce a 128-bit identifier with virtually no chance of collision. The python uuid generator guide includes production patterns for FastAPI, SQLAlchemy, and bulk generation scripts.
import java.util.UUID;
public class Main {
public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
System.out.println(uuid);
// Output: 550e8400-e29b-41d4-a716-446655440000
}
}
Explanation
import java.util.UUID;loads the JDK's built-in UUID class — no Maven or Gradle dependency needed.UUID.randomUUID()generates a random UUID (version 4) using a cryptographically strongSecureRandominstance internally.System.out.println(uuid)calls theUUIDobject'stoString()method automatically, producing the standard 36-character hyphenated format.- The result is a
UUIDobject, not aString— call.toString()explicitly when you need a string for concatenation, JSON serialization, or database storage.
Convert a String to a UUID in Java
Sometimes a UUID arrives as a plain string — from a REST request, a config file, or a database column — and you need it back as a proper UUID object. UUID.fromString() handles this in one line. The mysql uuid page includes examples for MySQL 8.0+ with UUID_TO_BIN() for storage-efficient identifiers.
import java.util.UUID;
public class Main {
public static void main(String[] args) {
UUID original = UUID.randomUUID();
String uuidString = original.toString();
// Parse the string back into a UUID object
UUID parsed = UUID.fromString(uuidString);
System.out.println(original.equals(parsed));
// Output: true
}
}
Explanation
UUID.fromString()is a static factory method that parses any valid 36-character hyphenated UUID string into aUUIDobject.- It throws
IllegalArgumentExceptionif the string is malformed, so wrap the call in atry/catchblock when parsing untrusted input. .equals()compares twoUUIDobjects by value, returningtruewhen they represent the same identifier regardless of how each was created.- Use this round-trip pattern to validate incoming UUIDs from API requests or database rows before trusting them.
Other Options for Generating UUIDs in Java
java.util.UUID covers random (v4) and name-based (v3) generation, but it has no native support for UUID v7, the newer time-sortable format increasingly used for database primary keys. The third-party uuid-creator library fills that gap.
<dependency>
<groupId>com.github.f4b6a3</groupId>
<artifactId>uuid-creator</artifactId>
<version>5.3.7</version>
</dependency>
import com.github.f4b6a3.uuid.UuidCreator;
import java.util.UUID;
public class Main {
public static void main(String[] args) {
// UUID v7 - Unix timestamp + random, sortable and index-friendly
UUID uuidV7 = UuidCreator.getTimeOrderedEpoch();
System.out.println(uuidV7);
}
}
Explanation
- Adding
com.github.f4b6a3:uuid-creatorto yourpom.xmlorbuild.gradlepulls in RFC 9562-compliant v7 generation that the JDK doesn't provide. UuidCreator.getTimeOrderedEpoch()encodes the current Unix millisecond timestamp in the most significant bits, so generated IDs sort chronologically — ideal for database primary keys and index locality.- The returned value is still a standard
java.util.UUID, so it works everywhere a JDK UUID does — JPA entities, JSON serialization,Comparablesorting. java.util.UUIDremains the right choice for v4 (random) and v3 (name-based) generation; reach foruuid-creatoronly when you specifically need v7 (or v1/v6) chronological ordering.