DEV Community

Cover image for How To Create UUID in Java?
Gaurav Kukade
Gaurav Kukade

Posted on • Originally published at coderolls.com

2

How To Create UUID in Java?

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

Introduction

UUID, 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 hyphen ("-") symbols. 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.

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 java.util package. And it has the static method randomUUID() which returns the randomly generated UUID.

The example is given below.

import java.util.UUID;

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

    public static void main(String[] args) {

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

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

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

    }

}

Enter fullscreen mode Exit fullscreen mode

Output

Printing the randomly generated UUID value......

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

Enter fullscreen mode Exit fullscreen mode

Get the above code as GitHub Gist.

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


You can visit my YouTube channel 'coderolls' to find more video tutorials.

Related Articles

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay