Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

uuid
java
communitycreator

How to generate random UUID in Java

abhilash

What is UUID?

The full form of UUID is Universally Unique Identifier. A UUID represents a 128-bit value that is unique. The standard representation of UUID uses hex digits.

For example:

3c0969ac-c6e3-40f2-9fc8-2a59b8987918
cb7125cc-d78a-4442-b21b-96ce9227ef51

UUID in Java

The UUID class in the java.util package bundles the method to generate random UUID.

There are four different versions of UUID. They are as follows:

  1. Time-based UUID
  2. DCE security UUID
  3. Name-based UUID
  4. Randomly generated UUID

This shot covers how to generate a randomly generated UUID.

Randomly generated UUID

The randomly generated UUID uses a random number as the source to generate the UUID.

In Java, the randomUUID() static method is used to generate a random UUID. The method internally uses SecureRandom class, which provides a cryptographically strong random number generator.

Every UUID is associated with a version number. The version number describes how the UUID was generated. The .version() method is used to get the version of the generated UUID.

Version number in java How the UUID was generated
1 Time-based UUID
2 DCE security UUID
3 Name-based UUID
4 Randomly generated UUID
import java.util.UUID;

public class GenUUID {
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        System.out.println("UUID generated - " + uuid);
        System.out.println("UUID Version - " + uuid.version());
    }
}

RELATED TAGS

uuid
java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring