Java Generate UUIDUUID 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. UUIDUUID 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 UUIDThere are four types of UUIDs:
Variants of UUIDThere 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 0x0000FFFFFFFFFFFF node 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 ClassFor 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 UUIDThe following example generates a random UUID. UUIDExample.java Output: 2b733549-d2cc-40f0-b7f3-9bfa9f3c1b89 Convert from a String to a UUIDStringToUUIDExample.java Output: 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 Next TopicLabeled Loop in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India