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).
The use of UUID depends on the situation, use cases, complexity, and conditions. Some of the uses of UUID are:
Types of UUID
There are four types of UUIDs:
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:
The LSBs consists of the following unsigned fields:
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
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 true