java.util.UUID class in Java
The java.util.UUID is a class in Java that depicts an immutable, universally unique identifier (UUID). The UUID contains a 128-bit label, which is utilized for information in the computer system. The UUID or GUID (Global unique identifier), is mostly utilized with Microsoft systems.
We have multiple different variants of UUID. The functions of the class are utilized for manipulating the Leach – Salz variant; however, the constructors enable the creation of any variant of UUID.
Some important points about the UUID are given below:
- The class depicts an immutable, universally unique identifier (UUID).
- The globally unique identifiers depict a unique 128-bit label.
- Its class is part of the java.util package.
- Its class implements the Serializable and Comparable interface
- The UUID class extends the Object class.
- The global identifiers are standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE).
UUID Variants
The variant number of UUID represents the layout of the UUID. Four different types of variants exist, but variant 2 is mostly used. The variant number and description are given below.
Number | Detail |
0 | It is reserved for the NCS backward compatibility. |
2 | IETF RFC 4122 (Leach-Salz) |
6 | Reserved, Microsoft corporation Backward compatibility. |
7 | It is reserved for future purposes. |
UUID Versions
The version number of UUID represents how the UUID was generated. Four versions of the UUID exist, which are explained in the table below.
Number | Detail |
1 | date-time-based and MAC address |
2 | IETF RFC 4122 (Leach-Salz) |
3 | DCE security namespace-name based |
4 | randomly produced UUID or GUID |
Note: In Java, only version 4 is supported. If required or needed for more options for producing the UUID, the usage of the java-uuid-generator jar is necessary.
Representation of UUID
The UUID is created using the hex digits and 4 hyphens. An example of a UUID is shown below.
52a39190-57f3-40aa-9c8f-42d8f0bc6cc8.
A representation of UUID is shown in the below image.
Name | Length(Hex digits) |
time_low | 8 |
time_mid | 4 |
time_high_and_version | 4 |
Variant_and_sequence | 4 |
Node | 12 |
Need for UUID
- It is utilized to generate a session ID for web application
- It can also be used to generate a transaction id
- Create random file names.
- Generate the Primary key for the databases.
Class Declaration
public final class UUID
extends Object
implements Serializable, Comparable<UUID>
Constructor Summary
We can create multiple UUIDs using the constructor given below.
Constructor | Detail |
UUID(long mostSigBits, long leastSigBits) | It creates a new UUID using the mentioned data. |
Method Summary
The method used with the UUID class is explained in the below section.
Sr.No. | Method | Type | Detail |
clockSequence() | int | It returns the clock sequence value linked with the UUID. | |
compareTo(UUID val) | int | It compares this UUID with the mentioned UUID. | |
equals(Object obj) | boolean | It compares this object to the mentioned object. | |
fromString(String name) | static UUID | It creates a UUID from the string standard representation specified in the toString() function. | |
getLeastSignificantBits() | long | It returns the least significant 64 bits of the UUID's 128-bit value. | |
getMostSignificantBits() | long | It returns the most significant 64 bits of the UUID's 128-bit value. | |
hashCode() | Int | It returns a hash code for the UUID. | |
nameUUIDFromBytes(byte[] name) | static UUID | It is a Static factory to get a type 3 (name based) UUID based on the mentioned byte array. | |
node() | long | It returns the node value linked with the UUID. | |
randomUUID() | static UUID | It is a static factory to get a type 4 (randomly generated) UUID. | |
timestamp() | long | It returns the timestamp value linked with the UUID. | |
toString() | String | It returns a String object representation of the UUID. | |
variant() | Int | It returns the variant number linked with the UUID. | |
version() | int | It returns the version number linked with the UUID. |
Inherited Methods
The UUID class inherits the methods from the class given below.
java.util.Object
The methods that are inherited from the Java.util.Object class are clone(), finalize(), getClass(), notify(), notifyAll(), wait(), wait(), wait().
Different ways to generate UUID
A UUID can be generated in Java using 3 different approaches explained in the below section.
1. Using String Standard Representation
The parameter here contains the name, a string specifying the UUID. It returns a UUID with the mentioned value.
UUID uuid = UUID.fromString("d41d4e2e-baf2-10e6-869b-6ff92833d2db");
2. Using the Pseudo Randomly Generated UUID.
The UUID here is generated using a cryptographically strong pseudo random number generator. It returns a randomly generated UUID.
UUID randomUUID=UUID.randomUUID();
3. Using the byte Array.
Here, we use a Static factory to get a type 3 (name-based) UUID that is based on the mentioned byte array.
byte[] byteArray = { 20, 10 ,5};
UUID uuid = UUID.nameUUIDFromBytes(byteArray);
Example 1
A new random UUID can be generated using the randomUUID() method illustrated in the example below.
FileName: UUIDExample.java
import java.util.UUID;
public class UUIDExample
{
// main method
public static void main(String[] args)
{
// Creates a new random UUID
UUID randomUUID=UUID.randomUUID();
System.out.println(randomUUID);
}
}
Output:
ed943889-175c-48a3-97b4-bf39d565dbe8
Example 2
The example below illustrates the clock sequence () method.
FileName: UUIDExample1.java
import java.util.UUID;
public class UUIDExample1
{
// main method
public static void main(String[] args)
{
// Creates a UUID from the string standard representation
UUID uuid = UUID.fromString("d41d4e2e-baf2-10e6-869b-6ff92833d2db");
// returns the clock sequence of the UUID
System.out.println(uuid.clockSequence());
}
}
Output:
1691
Example 3
The example below illustrates the version() method that returns the version value of the UUID.
FileName: UUIDExample2.java
import java.util.UUID;
public class UUIDExample2
{
// main method
public static void main(String arg[]) throws UnsupportedOperationException
{
// Creates a UUID from the string standard representation
UUID uuid1 =UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
UUID uuid2=UUID.fromString("5289df73-7df5-3326-bcdd-22597afb1fac");
// Returns and Displays the UUID version
System.out.println("The UUID version is: "+uuid1.version());
System.out.println("The UUID version is: "+uuid2.version());
}
}
Output:
The UUID version is: 1
The UUID version is: 3
Example 4
The below example illustrates the variant() method that returns the variant value of the UUID.
FileName: UUIDExample3.java
import java.util.UUID;
public class UUIDExample3
{
// main method
public static void main(String arg[]) throws UnsupportedOperationException
{
// Creates a UUID from the string standard representation
UUID uuid1 =UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
UUID uuid2=UUID.fromString("5289df73-7df5-3326-bcdd-22597afb1fac");
// Returns and Displays the UUID variant
System.out.println("The UUID variant is: "+uuid1.variant());
System.out.println("The UUID variant is: "+uuid2.variant());
}
}
Output:
The UUID variant is: 2
The UUID variant is: 2
Example 5
The below example illustrates the compareTo() method. It returns:
- 1 – when the uuid1 is greater than uuid2.
- 0 – when both the uuid are equal.
- -1 – when the uuid1 is smaller than uuid2.
FileName: UUIDExample4.java
import java.util.UUID;
public class UUIDExample4
{
// Main method
public static void main(String[] arg)
{
// generating random UUID
UUID uuid1 = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();
// compares this UUID with the mentioned UUID
int comparison = uuid1.compareTo(uuid2);
if(comparison == 1) {
System.out.println("uuid1 is greater than uuid2");
}
else if(comparison == 0) {
System.out.println("uuid and uuid are equal");
}
else {
System.out.println("uuid1 is smaller than uuid2");
}
}
}
Output:
uuid1 is smaller than uuid2
Example 6
The below examples illustrate the getMostSignificantBits(), getLeastSignificantBits(), and the hashCode() functions.
FileName: UUIDExample5.java
import java.util.UUID;
public class UUIDExample5
{
// Main method
public static void main(String[] arg)
{
// Creates a new random UUID
UUID randomUUID1 = UUID.randomUUID();
UUID randomUUID2 = UUID.randomUUID();
UUID randomUUID3 = UUID.randomUUID();
// Creates a UUID from the string standard representation
UUID uuid4 = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");
// Returns the most significant bit
System.out.println("The Most significant bit is: " + randomUUID1.getMostSignificantBits());
// Returns the least significant bit
System.out.println("The least significant bit is: " + randomUUID1.getLeastSignificantBits());
// Returns the Hash code for the UUID
System.out.println("The Hash code of the UUID is: " + randomUUID1.hashCode());
// Returns the Time stamp value linked with the UUID
System.out.println("The Time stamp of the UUID is: " + uuid4.timestamp());
}
}
Output:
The Most significant bit is: -4598635596546358620
The least significant bit is: -8668021075170578838
The Hash code of the UUID is: -1155288763
The Time stamp of the UUID is: 137004589606850094