Java UUID

Overview
In this article, we are going to learn about what is UUID in Java. So before getting started with the topic let us get a short overview of Java UUID.
Java UUID : In Java, we have the UUID class, which stands for Universally Unique Identifier. UUID is systemized by the Open Software Foundation (OSF). It is included in Distributive Computing Environment (DCE). A UUID is basically a 36 characters long unique number. We also call UUID a Globally Unique Identifier (GUID).
So let us now begin with the main agenda of our article, what is UUID in Java?
What is UUID in Java?
In Java, we have the UUID class, which stands for Universally Unique Identifier. The Java UUID class is imported from the java.util package. UUID is systemized by the Open Software Foundation (OSF). It is included in Distributive Computing Environment (DCE). A UUID is basically a 36 characters long unique number. We also call UUID a Globally Unique Identifier (GUID). Let us understand what is UUID in Java in points.
- The UUID class in Java stands for immutable Universally Unique Identifier (UUID).
- A UUID represents a 36-character, that is, a 128-bit long value that is distinctive to all practical purposes. All the information in the computer system is identified by the UUID.
- The Java UUID class is imported from the java.util package and it belongs to the package.
- The Serializable and Comparable interface can be implemented by the UUID class. Also, the UUID class can extend the Object class.
- The UUID global identifiers come in a wide variety. The Leach-Salz variant is manipulated by the methods of the UUID class.
Java UUID Representation
The Java UUID is represented with hex digits. Below given is a sample of how the Java UUID is represented.
Explanation:
- In the above representation, we can see the Java UUID is made up of hex digit along with four hyphens (-).
- The Java UUID is a 36 character long unique number, which includes four hyphens in it.
- We can also have a nil (void) UUID, in which all the bits of the UUID are set to zero.
Name | Length (Hex Digits) | Length (bytes) | Contents |
---|---|---|---|
time_low | 8 | 4 | low 32-bits of the time |
time_mid | 4 | 2 | middle 16-bits of the time |
time_hi and_version | 4 | 2 | 4-bit version in the MSB, followed by the high 12-bits of the time |
clock_seq_hi_and_res clock_seq_low | 4 | 2 | 1-3 bit variant in the most significant bits followed by the 13-15 bit clock sequence |
node | 12 | 6 | 48-bit node ID |
Declaration
Let us now understand how we can declare the UUID class in Java.
Explanation:
- In the above syntax, we are declaring the UUID class in Java.
- The UUID class is also a final class, that is, we cannot override, or inherit the methods of the UUID class in Java.
- The Serializable and Comparable interface can be implemented by the UUID class. Also, the UUID class can extend the Object class.
Java UUID Usage
Let us now discuss all the usage of Java UUID in points.
- It is used to create the Session ID for websites and web applications.
- It is used to create the Transaction ID.
- It is used to create the random file name.
- It is used to create the primary key for the database table as it is unique.
Java UUID Variants
The UUID global identifiers come in a wide variety. Let us discuss different variants of Java UUID.
- 0 : It is reserved for NCS(Network Computing System) backward compatibility. This variant is engaged for backward compatibility with the deprecated Apollo Network Computing System from the late 1980s. It has a similar construction to the Leach-Salz variant, which is used today.
- 1: Leach-Salz (it is the most used variant of Java UUID), these variants are also referred to as RFC 4122/DCE 1.1 UUIDs, after Engineering Task Force working document authors defining UUID specifications. For instance, GUIDs are one of the variant 1 UUIDs.
- 2: It is reserved for Microsoft backward compatibility. Although many of the GUIDs Microsoft uses are variant 1 UUIDs, variant 2 is used by early GUIDs on the Windows platform. The number of bits in the N position is the only difference between variant 1 and 2. Variant 2 UUIDs use three bits, whereas the variant 1 UUIDs use two bits.
Explanation:
In the above UUID code :
- "V" indicates the UUID version.
- "R" (1-3) indicate the UUID variant.
MSB1 | MSB2 | MSB3 | Description |
---|---|---|---|
0 | x | x | Reserved for NCS backward compatibility |
1 | 0 | x | Leach-Salz |
1 | 1 | 0 | Reserved for Microsoft backward compatibility |
1 | 1 | 1 | Reserved for future definition |
Leach-Salz variant: Using the Leach-Salz variant we can identify the layout of the UUID code. The representation of a variant 2 (Leach-Salz) UUID is as follows:
0xFFFFFFFF00000000 (indicates time_low) 0X0000000FFFF0000 (indicates time_mid) 0x0000000000000FF (indicates time_hi) 0x00000000000F000 (version)
The bit layout given above is valid only for a UUID with a variant value of 2 that indicates the Leach-Salz variant.
Java UUID Versions
Now let us discuss the different versions of UUID in Java. There are basically four different types of versions of Java UUID.
- time-based UUID (version 1)
- DCE security UUID (version 2)
- name-based UUID (version 3 and 5)
- randomly generated UUID (version 4)
Let us discuss them in detail.
time-based UUID and DCE security UUID (version 1 and version 2)
Version 1 is also called time-based UUID, as the IDs which are generated consist of the combination of DateTime values (the time at which the UUID is being generated), and also a part of the MAC address of the device in which the UUID is been generated.
Here’s how it breaks down visually:
Generating the UUID in this way is very advantageous, as it is kind of impossible to generate two UUIDs that are identical, as to do that we need to generate both the IDs by the same device at the exact same time and also have to generate the exact same random 16-bit sequence.
UUID v1 and UUID v2 IDS are also used to identify (for instance) the ID generated from which database node, just because a part of the generating device's MAC address is also included in the UUID. This is generally not a problem, and it can be an advantage in the case of distributed systems,
The only difference between v1 and v2 UUIDs is that a segment of a local domain number is also contained in the UUID v2, but not in the v1. For a number of reasons, this makes them less optimal for most applications, so UUID v2s are not widely used.
name-based UUID (Version 3 and version 5)
By hashing a namespace identifier and name, these two versions 3 and version 5 are generated. The name-based UUID is almost similar to the time-based UUID, as both are generated by using some existing data rather than being completely random, the only difference between them is the name-based UUID uses the namespace data and the name data, whereas the time-based UUID uses the DateTime data and the device's MAC address.
The namespace data is itself a UUID, and any random string can be used as name data for the UUID, however, it is preferred to use some name that will typically relate to how the UUID will be used. For example, it can be any account number or any product ID, or any identification number. But whatever the uses of those values, they are used to generate a 36-character alphanumeric string that is the final UUID.
The only difference between version 3 and version 5 is the different algorithms using which they are generated. Usually, for UUID v3 they use the MD5, and for UUID v3 they use the SHA-1 algorithm.
randomly generated UUID (Version 4 )
The term randomly generated UUID itself suggests, they are randomly generated 36 characters String. They are denoted as v4 (version 4) UUIDs, and each character of this UUID is a random character from a - zor an integer from0 - 9`.
They are extremely likely to be unique as they are generated with entirely random characters. This version is entirely different as it does not consist of any information like date time, or MAC address, or name data (which may be an advantage or a disadvantage depending on the specific use case).
Versions 6, 7, and 8
To date, none of these versions exist, but they can be added to the UUID standards in the coming years as they have been proposed. You can read more about these proposed new UUIDs here.
Constructors of Java UUID details
Different types of UUIDs are generated with the help of constructors of Java UUID. Let us look at the syntax of constructors of Java UUID.
Syntax :
Explanation:
- In the above syntax, we are creating a new instance of UUID, using the new keyword.
- Here the UUID class constructor is not the default one, but it has two parameters, let us discuss ahem.
- mostSigBits : This parameter consist of the most significant bits of the UUID. is used for the MSB (64-bit) of the UUID, and it is of a long type.
- leastSigBits : This parameter consist of the least significant bits of the UUID. is used for the LSB (64-bit) of the UUID, and it is of a long type.
Methods of Java UUID
Let us discuss the methods of the Java UUID class in form of a table.
Method | Description |
---|---|
int clockSequence() : | With the clockSequence() method, the clock sequence value is linked with the UUID. From the clock sequence field of the UUID, the 14-bit clock sequence value is constructed. To ensure the uniqueness of a time-based UUID, the clock sequence field is used. Only in time-based UUID (version 1), the clock sequence is useful. In case the UUID version is something other than time-based (version 1), then this method will throw an UnsupportedOperationException exception. |
int compareTo(UUID val) : | This compareTo() method of UUID class compares two UUIDs, that is it will compare this UUID with the given UUID as the parameter. The first UUID is greater than the second one, if the most significant field, where both the UUIDs are different is greater in the first UUID than the second one. |
boolean equals(Object obj) : | This equals() method of the UUID class compares two UUIDs, that is it will compare this object with the given object as the parameter. This method returns a boolean type value, if it returns true, then the argument is not null, it is a UUID object, has the same variant, and consists of the same value bit for a bit as this UUID. |
static UUID fromString(String name) : | This fromString() method of the UUID class creates a UUID from the toString() method, following the string standard representation. |
long getLeastSignificantBits() : | Using this getLeastSignificantBits() method of the UUID class we can get the least significant 64 bits of this UUID’s 128 bit value. |
long getMostSignificantBits() : | Using this getMostSignificantBits() method of the UUID class we can get the most significant 64 bits of this UUID’s 128 bit value. |
int hashCode() : | This hashCode() method of the UUID class returns the hash code of this UUID. |
static UUID nameUUIDFromBytes(byte[] name) : | This nameUUIDFromBytes() method of the UUID class is a static method, using which we can generate a type 3 (name based) UUID based on the specified byte array. |
long node() : | This node() method of the UUID class returns the node value which is linked with this UUID. From the node field of this UUID, this 48-bit node value is constructed. The main purpose of this field is to hold the IEEE 802 address of the machine that generated this UUID to guarantee spatial uniqueness. Only in time-based UUID (version 1), the node value is useful. In case the UUID version is something other than time-based (version 1), then this method will throw an UnsupportedOperationException exception. |
static UUID randomUUID() : | This randomUUID() method of the UUID class is a static method, using which we can generate a type 4 (pseudo-randomly generated) UUID. Using a cryptographically strong pseudo-random number generator, this UUID is generated. |
long timestamp() : | This timestamp() method of the UUID class returns the timestamp value linked with this UUID. From the time_low, time_mid, and time-hi fields of this UUID, the timestamp value of 60 bit is constructed. Basically, the returned timestamp is calculated in 100 nanosecond units since midnight, October 15, 1582, UTC. Only in time-based UUID (version 1), the timestamp value is useful. In case the UUID version is something other than time-based (version 1), then this method will throw an UnsupportedOperationException exception. |
String toString() : | This toString() method of the UUID class returns a String instance representing this UUID. |
int variant() : | This variant() method of the UUID class returns the variant number linked with the UUID. The layout of the UUID is described by the variant number. |
int version() : | This version() method of the UUID class returns the version number linked with this UUID. The way how the UUID was generated is described by the version number. The version number has the following meaning: Time-based UUID (version 1), DCE security UUID (version 2), Name-based UUID (version 3 and 5), Randomly generated UUID (version 4) |
Methods Detail
Let us discuss about all the methods of the UUID class in Java, in detail along with their syntax, and code.
int clockSequence() : With the clockSequence() method, the clock sequence value is linked with the UUID. From the clock sequence field of the UUID, the 14-bit clock sequence value is constructed. To ensure the uniqueness of a time-based UUID, the clock sequence field is used. Only in time-based UUID (version 1), the clock sequence is useful. In case the UUID version is something other than time-based (version 1), then this method will throw an UnsupportedOperationException exception.
Syntax :
Return Type :
Exception :
Code :
Output :
Explanation :
- In the above code, firstly we are importing the UUID class from the java.util package.
- The main method, throws an UnsupportedOperationException, which will throw an exception if the version of the UUID is something other than time-based (version 1).
- Inside the main method we are generating the UUID, with the help of the fromString() method, which will create a UUID from the toString() method, following the string standard representation.
- Lastly, we are calling the getClockSequence, where we are checking the clock sequence of the UUID, using the clockSequence() method, and printing it.
int compareTo(UUID val) : This compareTo() method of UUID class compares two UUIDs, that is it will compare this UUID with the given UUID as the parameter. The first UUID is greater than the second one, if the most significant field, where both the UUIDs are different is greater in the first UUID than the second one.
Syntax:
Return Type:
Exception:
**Code **
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method, we are generating two random UUIDs with the help of the randomUUID() method, and storing them in two different variables of UUID type val1, and val2.
- Then we are calling the compare, where we are compare both the UUID val1, and val2, with the help of the compareTo() method.
- It will return -1, 0, or 1 if val1 UUID is less than, equal to, or greater than val2.
boolean equals(Object obj): This equals() method of the UUID class compares two UUIDs, that is it will compare this object with the given object as the parameter. This method returns a boolean type value, if it returns true, then the argument is not null, it is a UUID object, has the same variant, and consists of the same value bit for a bit as this UUID.
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method, we are generating two random UUIDs with the help of the randomUUID() method, and storing them in two different variables of UUID type val1, and val2.
- Then we are calling the isEqual, where we are checking if both the UUID val1, and val2 are equal or not, with the help of the equals() method.
- It will return true if both the objects val1 and val2 are the same, and it will return false if both the objects val1 and val2 are not the same.
static UUID fromString(String name) : This fromString() method of the UUID class creates a UUID from the toString() method, following the string standard representation.
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Then we are calling the generateUUID, were we are generating a random UUID, with the help of the fromString() method.
- Then we are printing the value.
long getLeastSignificantBits() : Using this getLeastSignificantBits() method of the UUID class we can get the 64 bits least significant of this UUID’s 128 bit value.
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method, we are generating a random UUID with the help of the randomUUID() method, and storing it in variables of UUID type val.
- Then we are calling the leastSignificant, were we are checking the least significant bit, with the help of the getLeastSignificantBits() method.
- Then we are printing its value.
long getMostSignificantBits() : Using this getMostSignificantBits() method of the UUID class we can get the 64 bits most significant of this UUID’s 128 bit value.
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method, we are generating a random UUID with the help of the randomUUID() method, and storing it in variables of UUID type val.
- Then we are calling the mostSignificant where we are checking the most significant bit, with the help of the getMostSignificantBits() method.
- Then we are printing its value.
int hashCode() : This hashCode() method of the UUID class returns the hash code of this UUID.
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method, we are generating a random UUID with the help of the randomUUID() method, and storing it in variables of UUID type val.
- Then we are calling the hashcode, where we are checking the hash code for this UUID, with the help of the hashCode() method.
- Then we are printing its value.
static UUID nameUUIDFromBytes(byte[] name) : This nameUUIDFromBytes() method of the UUID class is a static method, using which we can generate a type 3 (name based) UUID based on the specified byte array.
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method, we are creating an array of byte types, and storing it in variables of byte[] type b.
- Then we are calling the generateUUID, where we are creating the UUID from the byte array b and storing it in variables of UUID type val.
- Then we are printing its value.
long node() : This node() method of the UUID class returns the node value which is linked with this UUID. From the node field of this UUID, this 48-bit node value is constructed. The main purpose of this field is to hold the IEEE 802 address of the machine that generated this UUID to guarantee spatial uniqueness. Only in time-based UUID (version 1), the node value is useful. In case the UUID version is something other than time-based (version 1), then this method will throw an UnsupportedOperationException exception.
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method we are generating the UUID, with the help of the fromString() method, which will create a UUID from the toString() method, following the string standard representation, and storing it in variables of UUID type val.
- Then we are calling the getNode, where we are checking node value for this UUID, with the help of the node() method.
- Then we are printing its value.
static UUID randomUUID() : This randomUUID() method of the UUID class is a static method, using which we can generate a type 4 (pseudo-randomly generated) UUID. Using a cryptographically strong pseudo-random number generator, this UUID is generated.
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method, we are calling the generateRandomUUID, where we are generating a random UUID with the help of the randomUUID() method, and storing it in variables of UUID type val.
- Then we are printing its value.
long timestamp() : This timestamp() method of the UUID class returns the timestamp value linked with this UUID. From the time_low, time_mid, and time-hi fields of this UUID, the timestamp value of 60 bit is constructed. Basically, the returned timestamp is calculated in 100 nanosecond units since midnight, October 15, 1582, UTC. Only in time-based UUID (version 1), the timestamp value is useful. In case the UUID version is something other than time-based (version 1), then this method will throw an UnsupportedOperationException exception.
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method we are generating the UUID, with the help of the fromString() method, which will create a UUID from the toString() method, following the string standard representation, and storing it in variables of UUID type val.
- Then we are calling the timestamp, where we are checking the time stamp for this UUID, with the help of the timestamp() method.
String toString() : This toString() method of the UUID class returns a String instance representing this UUID.
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method we are generating the UUID, with the help of the fromString() method, which will create a UUID from the toString() method, following the string standard representation, and storing it in variables of UUID type val.
- Then we are calling the getToString, where we are checking the string format for this UUID, with the help of the toString() method.
int variant() : This variant() method of the UUID class returns the variant number linked with the UUID. The layout of the UUID is described by the variant number.
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method we are generating the UUID, with the help of the fromString() method, which will create a UUID from the toString() method, following the string standard representation, and storing it in variables of UUID type val.
- Then we are calling the getVarient, where we are checking the variant number for this UUID, with the help of the variant() method.
int version() : This version() method of the UUID class returns the version number linked with this UUID. The way how the UUID was generated is described by the version number. The version number has the following meaning: Time-based UUID (version 1), DCE security UUID (version 2), Name-based UUID (version 3 and 5), Randomly generated UUID (version 4)
Syntax:
Return Type:
Exception:
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method we are generating the UUID, with the help of the fromString() method, which will create a UUID from the toString() method, following the string standard representation, and storing it in variables of UUID type val.
- Then we are calling the getVersion, where we are checking the version for this UUID, with the help of the version() method.
How to Generate a UUID in Java?
In Java programming language, we have a built-in method with which we can generate random UUIDs of version 4. Let us look at an example of how we can generate a UUID in Java.
Code:
Output:
Explanation:
- In the above code, firstly we are importing the UUID class from the java.util package.
- Inside the main method, we are generating a random UUID with the help of the randomUUID()method, and storing it in variables ofUUID type UUID.
- Then we are converting the UUID to String with the help of the toString() method.
- Then we are printing its value.
Java UUID Examples
Let us look at some examples of UUID classes in Java.
Code:
Output:
Conclusion
In this article, we learned about how to get the Java UUID class. Let us recap the points we discussed throughout the article:
- The UUID class in Java stands for immutable Universally Unique Identifier (UUID).
- A UUID represents a 36 character, that is, a 128-bit long value that is distinctive to all practical purposes. All the information in the computer system is identified by the UUID.
- The UUID global identifiers come in a wide variety. The Leach-Salz variant is manipulated by the methods of the UUID class.
- UUID is used to create the Session ID for websites and web applications.
- UUID is used to create the Transaction ID.
- UUID is used to create the primary key for database table as it is unique.
- The UUID global identifiers come in a wide variety, they are 0, 2, 6, and 7.
- There are basically four different types of versions of Java UUID, they are time-based UUID (version 1), DCE security UUID (version 2), name-based UUID (version 3 and 5), randomly generated UUID (version 4).
- Some of the methods of Java UUID class are : int clockSequence(), int compareTo(UUID val), boolean equals(Object obj), static UUID fromString(String name), etc.