DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong

Trending

  • Building a Software Factory With AI Agents and Workflows
  • Grounding AI Agents in Governed Data
  • How to Format Articles for DZone
  • What Cloud Engineers Actually Need to Know About AI Infrastructure

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. How To Create a UUID in Java?

How To Create a UUID in Java?

In this article, you will see how to create a UUID in Java. We are going to create a Randomly Generated UUID, i.e., version 4 UUID here.

By 
Gaurav Kukade
·
Jan. 02, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In this article, you will see how to create a UUID in Java.

Introduction

A UUID, or a universally unique identifier, is a 128-bit number used to identify information in computer systems.

UUID is made of hex digits along with 4 hyphens (-). The length of a UUID is 36 characters.

There are 5 types of UUID, but mostly version 4, i.e., Randomly Generated UUID, is used.

We are going to create the version 4 UUID here.

If you do not want to read, you can watch the short video on How to create UUID in Java?

Example UUID

`df6fdea1-10c3-474c-ae62-e63def80de0b`

How to create UUID in Java?

Creating a Randomly Generated UUID (version 4) is really easy in Java.

UUID class is present in thejava.utilpackage. And it has the static methodrandomUUID()which returns the randomly generated UUID.

The example is given below:

Java
 




x
22


 
1
import java.util.UUID;
2

          
3
/**
4
 * A Java program to create a Randomly Generated UUID i.e. Version 4 UUID
5
 * @author Gaurav Kukade at coderolls.com
6
 *
7
 */
8
public class CreateUUID {
9

          
10
    public static void main(String[] args) {
11

          
12
        // creating random uuid i.e. version 4 UUID
13
        UUID uuid = UUID.randomUUID();
14

          
15
        System.out.println("Printing the randomly generated UUID value......\n");
16

          
17
        System.out.println("uuid: "+uuid);
18

          
19
    }
20

          
21
}



Output: 

Java
 




xxxxxxxxxx
1


 
1
Printing the randomly generated UUID value......
2

          
3
uuid: e3aed661-ccc2-42fd-ad69-734fee350cd2



Get the above code as GitHub Gist.

This article was originally published at coderolls.com/create-uuid-in-java

Java (programming language)

Published at DZone with permission of Gaurav Kukade. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook