Generate a UUID in Java

Java is one of the most popular programming languages in the world!

Since its humble beginnings at Sun Microsystems in 1991, Java has come to dominate enterprise backend software, run the majority of smart phones (Android), and be used extensively on desktop computers in apps such as LibreOffice and Minecraft.

How to Generate a UUID in Java

The Java language has built-in support for generating Version 4 UUIDs. Here's an example of how you can create a UUID in Java code.

import java.util.UUID;
class MyUuidApp {
public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
String uuidAsString = uuid.toString();
System.out.println("Your UUID is: " + uuidAsString);
}
}

Explanation

  • On line #1, we import the UUID class. This class is part of the standard Java JDK. No 3rd party libraries are required.
  • On line #5, the UUID class' static method, randomUUID(), is used to generate a new, version 4 UUID. The generated UUID object is stored in the variable, uuid.
  • Line #6 converts the uuid object into a Java String by calling its toString() method. The String representation of a UUID looks like a standard UUID (i.e. f8c3de3d-1fea-4d7c-a8b0-29f63c4c3454). If you're going to store the UUID in a file, database or model property, or send it via an API call to a different application, you'll almost always need the String representation of the uuid object, rather than the uuid object itself.
  • The output from line #8 will be something like:
    Your UUID is: f8c3de3d-1fea-4d7c-a8b0-29f63c4c3454
    

Convert from a String to a UUID in Java

Although it's rare, in some circumstances you might need to convert from a String representation of a UUID (like the one from line #6 above) back into an instance of UUID.

Java's UUID class provides for this scenario with the static method, fromString(String). You can call this method like this:

import java.util.UUID;
class MyUuidApp {
public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
String uuidAsString = uuid.toString();
UUID sameUuid = UUID.fromString(uuidAsString);
assert sameUuid.equals(uuid);
}
}

Explanation

  • Line #8 shows converting the string representation of a UUID into a Java UUID instance (sameUuid) using the fromString(String) method.
  • Line #9 is included to show that the 2 UUID instances are equal.

Other Options for Generating UUIDs in Java

The uuid-creator library provides support for generating many different types of UUIDs.

For example, to generate version 7 UUIDs with this library, you can:

import com.github.f4b6a3.uuid.UuidCreator;
class MyUuidApp {
public static void main(String[] args) {
UUID uuid = UuidCreator.getTimeOrderedEpoch();
String uuidAsString = uuid.toString();
System.out.println("Your UUID is: " + uuidAsString);
}
}

Explanation

  • On line #1, we import the UuidCreator class that is part of the uuid-creator library.
  • On line #5, the UuidCreator class' static method, getTimeOrderedEpoch(), is used to generate a new, version 7 UUID.
  • The output from line #8 will be something like:
    Your UUID is: 018b2f19-e79e-7d6a-a56d-29feb6211b04
    

How can we improve this page? Let us know!




The UUIDs generated by this site are provided AS IS without warranty of any kind, not even the warranty that the generated UUIDs are actually unique. You are responsible for using the UUIDs and assume any risk inherent to using them. You are not permitted to use the UUIDs generated by this site if you do not agree to these terms. Do not use any UUIDs found on cached versions of this page.
Privacy Policy
This website uses cookies. We use cookies to personalise content/ads and to analyse our traffic.

The UUIDs generated by this site conform to RFC 4122 whenever possible.
Read more about UUIDs at Wikipedia.
Check out our developer API.

Copyright © 2024 TransparenTech LLC. All Rights Reserved. Contact Us.