Java

What Is UUID in Java

The acronym “UUID” stands for “Universal Unique Identifier” which is an immutable class. A UUID represents a 128-bit value. The UUID class expands the Object class and defines the Serializable and Comparable interfaces. It is a unique number with 36 characters, including 4 hyphens and a hex digit (-). A UUID can be empty, which sets all bits to zero. UUIDs come in four main fundamental categories which include time-based, the name-based, DCE security, and randomly generated UUID values. Some methods of the UUID class are given in the following examples to get the UUID values.

Example 1:

The random() method is provided by the UUID class which generates the random UUID. This method creates a different UUID with each execution. A random number is used as the source to create the UUID in the randomly generated UUID.

We imported the UUID class package in the previous code. Then, we established the “UUIDMethod1” class. The Java class encloses the main() method for the source code of the UUID. The object “obj” is declared by specifying the UUID class inside the main() method’s section. The UUID “obj” object class is assigned with the UUID’s “randomUUID()” method. Note that the “randomUUID()” method doesn’t take any single parameter but generates the random UUID on execution time. The randomly generated UUID results are displayed by the println statement as the UUID object is passed as an argument.

The random UUID is generated by the “randomUUID()” method in the following snap of the terminal screen:

Example 2:

The version() method corresponds to the UUID. The version information identifies the creation process for this UUID. The UUID number version is obtained from the version() method.

We have an import package statement as the initial step in the given program. After that, we have the formation of the Java “UUIDMethod2” class where the main() method of the program is called out with the “UnsupportedOperationException” exception class to support the error handling of this program. Next, we declare two objects – “uuid1” and “uuid2” – of the UUID class. We use the fromString() method to input the UUID strings. Then, we deploy the version() method in the println() method for the UUID objects, “uuid1” and “uuid2”, which generate the version of the specified UUID string.

The UUID version is displayed for the assigned values. Version “1” shows the time-based UUID and version “3” shows the name-based UUID.

Example 3:

The relevant variant number with this UUID can be acquired using the variant() method. The variation number gives data about the structure of the UUID.

We generate the “UUIDMethod3” class in the given program and build the main() method there. Next, we declare the UUID class “MyUUID” object in which the fromString() method is deployed, and the string UUID is specified inside it. After that, we use the println() method to call out the variant() method and display the variant value. The variant() method is employed with the “MyUUID” object to generate the variant number of the specified UUID string.

The variant number “2” is obtained for the given string format UUID on the following screen:

Example 4:

The node() method of UUID gets the node value of the specified UUID. This method of UUID throws an “UnsupportedOperationException” if the provided UUID is not time-based. It only accepts the time-based UUID for the node value.

We develop the “UUIDMethod4” class which wraps the main() method for the UUID method implementation. There, we define the “U1” object for the UUID class and initialize it with the formString() method. The fromString() method takes the UUID value in string format. Then, we use the node() method for the “U1” object to fetch the node value of the specified UUID. The node() method results are printed by the println() method because we deploy this method inside it.

The node value of the provided UUID is returned by the node() method which is displayed in the following compilation of the Java program:

Example 5:

The most commonly used method to acquire the Hash code value of the UUID in Java is the hashCode() method which is provided by the UUID class. No arguments are required for this hashCode() method. The Hash code for this UUID object is returned by this method as an integer value.

We deploy the Java class “UUIDMethod5” after the import statement of the UUID package. Then, we add the main() method in the class where the “uuid” object is declared for the UUID class. We deploy the randomUUID() method in that object to generate the random value of UUID. After getting the random UUID, we obtaine the Hash code of that UUID by calling the hashCode() method within the print statement. The hashCode() gets the UUID from the “uuid” object and provides the integer hash code for the random UUID value.

The hashCode() method displays the Hash code results for the randomly generated UUID by the randomUUID() method.

Example 6:

The fromString() method creates the timeStamp UUID value from the string expression or the UUID value associated with this particular UUID string. The timeStamp() method is only used with the class object. If we try to use the class name to retrieve the method, an error is raised.

We create the Java class “UUIDMethod6” where the main() method is set. The main() method has the further implementation of the Java code. We first define the “res” object which is the object of the UUID class. Then, we employ the fromString() method which is specified with the string format of UUID. We have another object “timeStamp” declaration to use the timeStamp() method. The timeStamp() method returns the timeStamp result of this UUID.

The stamp UUID is returned from the UUID’s “timeStamp()”class method as follows:

Conclusion

The UUID class is supported by the Java package which is used to represent the UUID values associated with this class. It is used to generate the arbitrary file names, session identifiers for web applications, transaction identifiers, etc. There are several methods of Java UUID class that are extensively discussed here. All these methods are used to give the UUID value but in different formats.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.