Javatpoint Logo
Javatpoint Logo

Java Generate UUID

UUID is a widely used 128-bit long unique identification number in the computer system. It consists of hex-digits separated by four hyphens. In this section, we will discuss what is UUID and how to randomly generate UUID (version 4) in Java.


UUID stands for Universally Unique IDentifier. UUIDs are standardized by the Open Software Foundation (OSF). It is a part of the Distributive Computing Environment (DCE). A UUID is 36 characters (128-bit) long unique number. It is also known as a Globally Unique IDentifier (GUID).

Java Generate UUID

The use of UUID depends on the situation, use cases, complexity, and conditions. Some of the uses of UUID are:

  • It is used to generate unique URN (Uniform Resource Names).
  • It can also be used to generate transaction IDs.
  • Widely used in a cryptographic application.
  • In a web application it can be used to generate session IDs.

Types of UUID

There are four types of UUIDs:

  • Time-based UUID (version 1)
  • DCE security UUID (version 2)
  • Name-based UUID (version 3 and 5)
  • Randomly Generated UUID (version 4)

Variants of UUID

There are many variants of the UUID but Leach-Salz variant is widely used. The layout of variant 2 i.e. Leach-Salz is as follows:

The MSBs consists of the following unsigned fields:

0xFFFFFFFF00000000 time_low

0x00000000FFFF0000 time_mid

0x000000000000F000 version

0x0000000000000FFF time_hi

The LSBs consists of the following unsigned fields:

0xC000000000000000 variant

0x3FFF000000000000 clock_seq


The variant field holds a value that identifies the layout of the UUID. The above-discussed layout is valid only for variant 2.

Java UUID Class

For generating the UUID, the Java programming language provides the UUID class. The class belongs to java.util package. It extends the Object class and implements the serializable and comparable<UUID> interface. The class generates an immutable UUID that represents a 128-bit value.

Java UUID Class Methods

Methods Description
clockSequence() It returns the clock sequence value associated with this specified UUID.
compareTo() The method compares the UUID with the specific UUID.
equals() The method compares this object to the specified object.
fromString() It generates a UUID from the String representation.
getLeastSignificantBits() It returns the least significant 64 bits of this UUID's 128-bit value.
getMostSignificantBits() It returns the most significant 64 bits of this UUID's 128-bit value.
hashCode() It returns a hash code for this UUID.
nameUUIDFromBytes() It retrieves a version-3 (name-based) UUID based on the specified byte array.
node() It returns a node value that is associated with the specified UUID.
randomUUID() It returns a randomly generated UUID.
timestamp() It returns a timestamp value associated with this specified UUID.
toString() It returns a String object representing this UUID.
variant() It is used to get the variant associated with the specified UUID.
version() We can get the version number associated with the specified UUID.

Generating a UUID

The following example generates a random UUID.



Convert from a String to a UUID


Randomly Generated UUID: fe8a03d7-6495-4231-9843-8ee2f5282620
UUID as String: fe8a03d7-6495-4231-9843-8ee2f5282620
UUID from String: fe8a03d7-6495-4231-9843-8ee2f5282620

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA