How to Generate a UUID in Java with UUID.randomUUID
UUID.randomUUID() from java.util returns a new random UUID every time you call it. It returns a UUID object, and toString() turns it into the familiar 36 characters, such as 411522a0-fa94-4b23-a254-63a0f496813d.
Syntax
import java.util.UUID;
UUID <id> = UUID.randomUUID();
String <text> = <id>.toString();
| Part | What it does |
|---|---|
import java.util.UUID | Makes the UUID class available by its short name. |
UUID.randomUUID() | Makes a new random UUID. |
toString() | Returns the UUID as text in the usual form with dashes. |
The UUID documentation for randomUUID(): "Static factory to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator."
The Example
The example makes a UUID and prints it, its version and the length of its text, then makes a second UUID.
Prerequisites
- A JDK 11 or later, which can run a single
.javafile directly. This guide uses OpenJDK 25.0.5.1.
Step 1: Write the Code
Create a file named Main.java:

import java.util.UUID;
public class Main {
public static void main(String[] args) {
UUID id = UUID.randomUUID();
System.out.println(id);
System.out.println(id.version());
System.out.println(id.toString().length());
System.out.println(UUID.randomUUID());
}
}
Step 2: Run It
java Main.java
411522a0-fa94-4b23-a254-63a0f496813d
4
36
bf7e2f8e-1732-483c-ac1e-aa3cd2c7aea5

The command is the same in zsh, bash and PowerShell. Your UUIDs will differ, because they're random.
What Happened
println(id)printed the UUID as text, becauseprintlncallstoString()for you.version()returned4. The documentation lists the versions, and4means "Randomly generated UUID".- The text is 36 characters long: 32 hexadecimal digits and four dashes.
- The second call made a different UUID.
Where text is expected, for example a JSON Web Token's jti claim, pass UUID.randomUUID().toString().
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Forgetting the import | error: cannot find symbol for UUID | Add import java.util.UUID; |
Comparing two UUIDs with == | It compares the objects, not their values | Use equals |
Using UUID.nameUUIDFromBytes for random IDs | It makes the same UUID from the same bytes every time | Use UUID.randomUUID() |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error: cannot find symbol pointing at UUID | java.util.UUID isn't imported | Add the import |
IllegalArgumentException: Invalid UUID string | UUID.fromString got text that isn't a UUID | Check the text before converting it |
FAQ
Is randomUUID random enough for IDs that must not be guessed?
Yes. The documentation says it uses "a cryptographically strong pseudo random number generator". Don't use a UUID as a password or a key, though: anyone who sees it can copy it.
How do I turn text back into a UUID?
UUID.fromString("411522a0-fa94-4b23-a254-63a0f496813d") returns the UUID object for that text.
Conclusion
UUID.randomUUID() makes a random version 4 UUID, and toString() gives you its 36 characters of text.
Additional Resources
- UUID, in the Java 25 API documentation.
- RFC 9562 section 5.4, the UUID version 4 format.
