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 itstoString()
method. The String representation of aUUID
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 theuuid
object, rather than theuuid
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 thefromString(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!