Generate UUIDs in Java
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: Java
What: Generate UUIDs in Java using the built-in <code>java.util.UUID</code> class. Provides UUID v4 (random) and v3 (namespaced) generation with no external dependencies.
Try it: Use our interactive Uuids generator or integrate this code into your Java application.
Generate UUIDs in Java using the built-in java.util.UUID class. Provides UUID v4 (random) and v3 (namespaced) generation with no external dependencies.
Looking for other languages? Check our code examples in
PHP
,
JavaScript
,
Python
,
C#
,
C++
,
Ruby
and
Go
or use our interactive web generator.
Java Code Example
import java.util.UUID;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class UuidExample {
public static void main(String[] args) {
// UUID v4 (random)
UUID uuid4 = UUID.randomUUID();
System.out.println(uuid4); // Example: 3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f
// UUID v3 (namespaced with MD5)
UUID uuid3 = generateUuidV3("example.com");
System.out.println(uuid3); // Always same: 9073926b-929f-31c2-abc9-fad77ae3e8eb
// Parse UUID from string
String uuidString = "3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f";
UUID parsed = UUID.fromString(uuidString);
System.out.println(parsed.version()); // Outputs: 4
// Generate multiple UUIDs
for (int i = 0; i < 5; i++) {
System.out.println(UUID.randomUUID());
}
}
// Helper method for UUID v3 generation
private static UUID generateUuidV3(String name) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hash = md.digest(name.getBytes(StandardCharsets.UTF_8));
hash[6] &= 0x0f; // Clear version
hash[6] |= 0x30; // Set to version 3
hash[8] &= 0x3f; // Clear variant
hash[8] |= 0x80; // Set to IETF variant
return UUID.nameUUIDFromBytes(hash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
[EXPLANATION]
UUID.randomUUID() uses SecureRandom for cryptographically secure generation. Java's built-in UUID class supports v4 (random) natively. For v3 (namespaced), use nameUUIDFromBytes() with MD5 hashing.
Expected Output
3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f 9073926b-929f-31c2-abc9-fad77ae3e8eb 4
Common Use Cases
- Spring Boot entity IDs
- Hibernate/JPA primary keys
- Kafka message keys
- Android app unique identifiers
- Microservices transaction IDs
- Cassandra partition keys
Important Notes
-
UUID.randomUUID()is thread-safe - Java UUIDs are RFC 4122 compliant
-
Use
toString()for string representation - Consider using UUID as database primary keys with proper indexing
Try Our Interactive Generator
Don't want to write code? Use our free web-based Uuids generator with instant results.
TRY UUIDS GENERATOR →Other Programming Languages
View Uuids generation code examples in PHP
View Uuids generation code examples in JavaScript
View Uuids generation code examples in Python
View Uuids generation code examples in C#
View Uuids generation code examples in C++
View Uuids generation code examples in Ruby
View Uuids generation code examples in Go