Skip to main content

How to Generate a UUID in Java with UUID.randomUUID

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
import java.util.UUID;

UUID <id> = UUID.randomUUID();
String <text> = <id>.toString();
PartWhat it does
import java.util.UUIDMakes 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 .java file directly. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which imports java.util.UUID, stores UUID.randomUUID() in id, prints it, its version and the length of its text, then prints a second UUID.randomUUID().

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​

Terminal
java Main.java
Output
411522a0-fa94-4b23-a254-63a0f496813d
4
36
bf7e2f8e-1732-483c-ac1e-aa3cd2c7aea5

A zsh terminal where java Main.java prints a UUID, the version 4, the length 36 and a second, different UUID.

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, because println calls toString() for you.
  • version() returned 4. The documentation lists the versions, and 4 means "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​

MistakeWhat happensDo this instead
Forgetting the importerror: cannot find symbol for UUIDAdd import java.util.UUID;
Comparing two UUIDs with ==It compares the objects, not their valuesUse equals
Using UUID.nameUUIDFromBytes for random IDsIt makes the same UUID from the same bytes every timeUse UUID.randomUUID()

Troubleshooting​

SymptomLikely causeFix
error: cannot find symbol pointing at UUIDjava.util.UUID isn't importedAdd the import
IllegalArgumentException: Invalid UUID stringUUID.fromString got text that isn't a UUIDCheck 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​