Java.util.UUID class in Java

19 Jan, 2022
A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value. There exist different variants of these global identifiers. The methods of this class are for manipulating the Leach-Salz variant, although the constructors allow the creation of any variant of UUID (described below). There are four different basic types of UUIDs: time-based, DCE security, name-based, and randomly generated UUIDs. These types have a version value of 1, 2, 3 and 4, respectively. 
Used to create session id in web application. It is also used to create transaction id.
It extends Object class.
It implements Serializable and Comparable interfaces.
Constructor:  
UUID(long mostSigBits, long leastSigBits)
Constructs a new UUID using the specified data. mostSigBits – The most significant bits of the UUID leastSigBits – The least significant bits of the UUID 
Methods: 
int clockSequence(): The clock sequence value associated with this UUID. The 14 bit clock sequence value is constructed from the clock sequence field of this UUID. The clock sequence field is used to guarantee temporal uniqueness in a time-based UUID. The clockSequence value is only meaningful in a time-based UUID, which has version type 1. If this UUID is not a time-based UUID then this method throws UnsupportedOperationException.
Syntax: public int clockSequence().
Returns: The clock sequence of this UUID.
Exception: 
UnsupportedOperationException - If this UUID is not a version 1 UUID
Java
// Java code illustrating clockSequence() methodimport java.util.UUID;class UUIDdemo{    public static void main(String arg[]) throws            UnsupportedOperationException    {        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");                 // checking clock sequence        System.out.println(gfg.clockSequence());    }}
Output: 
1691
int compareTo(UUID val): Compares this UUID with the specified UUID. The first of two UUIDs is greater than the second if the most significant field in which the UUIDs differ is greater for the first UUID.
Syntax: public int compareTo(UUID val).
Returns: -1, 0 or 1 as this UUID is less than, equal to, or greater than val.
Exception: NA.
Java
// Java code illustrating compareTo() methodimport java.util.UUID;class UUIDdemo{    public static void main(String arg[])    {        // generating random UUID        UUID gfg1 = UUID.randomUUID();        UUID gfg2 = UUID.randomUUID();                 int compare = gfg1.compareTo(gfg2);        if(compare==1)            System.out.println("gfg1 is greater than gfg2");        else if(compare==0)            System.out.println("both are equal");        else            System.out.println("gfg1 is smaller than gfg2");      }}
Output: 
gfg1 is smaller than gfg2
boolean equals(Object obj): Compares this object to the specified object. The result is true if and only if the argument is not null, is a UUID object, has the same variant, and contains the same value, bit for bit, as this UUID.
Syntax: public boolean equals(Object obj).
Returns: true if the objects are the same; false otherwise
Exception: NA.
Java
// Java code illustrating equals() methodimport java.util.UUID;class UUIDdemo{    public static void main(String arg[])    {        // generating random UUID        UUID gfg1 = UUID.randomUUID();        UUID gfg2 = UUID.randomUUID();                 if(gfg1.equals(gfg2))            System.out.println("both are equal");        else            System.out.println("both are not same");    }}
Output: 
both are not same
static UUID fromString(String name): Creates a UUID from the string standard representation as described in the toString() method.
Syntax: public static UUID fromString(String name).
Returns: a UUID with the specified value.
Exception: 
IllegalArgumentException - If name does not conform to the string 
representation as described in toString()
Java
// Java code illustrating fromString() methodimport java.util.UUID;class UUIDdemo{    public static void main(String arg[])    {        // generating random UUID        UUID gfg = UUID.fromString("e52232e1-0ded-4587-999f-4dd135a4a94f");        System.out.println("UUID is: " + gfg);    }}
Output: 
UUID is: e52232e1-0ded-4587-999f-4dd135a4a94f
long getLeastSignificantBits(): This method returns the least significant 64 bits of this UUID’s 128 bit value.
Syntax: public long getLeastSignificantBits().
Returns: The least significant 64 bits of this UUID's 128 bit value.
Exception: NA.
Java
// Java code illustrating getLeastSignificantBits() methodimport java.util.UUID;class UUIDdemo{    public static void main(String arg[])    {        UUID gfg = UUID.randomUUID();                 // checking the least significant bit        System.out.println("Least significant bit " +                gfg.getLeastSignificantBits());    }}
Output: 
Least significant bit -8406445530268383532
long getMostSignificantBits(): This method returns the most significant 64 bits of this UUID’s 128 bit value.
Syntax: public long getMostSignificantBits()
Returns: The most significant 64 bits of this UUID's 128 bit value.
Exception: NA
Java
// Java code illustrating getMostSignificantBits() bitimport java.util.UUID;class UUIDdemo{    public static void main(String arg[])    {        UUID gfg = UUID.randomUUID();                 // checking the most significant bit        System.out.println("Most significant bit " +                gfg.getMostSignificantBits());    }}
Output: 
Most significant bit 8138958362250724568
int hashCode(): this method returns hash code for this UUID.
Syntax: public int hashCode().
Returns: the hash code for this UUID.
Exception: NA.
Java
// Java code illustrating hashCode methodimport java.util.UUID;class UUIDdemo{    public static void main(String arg[])    {        UUID gfg = UUID.randomUUID();                 // checking the hash code for this UUID        System.out.println("Hash code " +                gfg.hashCode());    }}
Output: 
Hash code -2073067668
static UUID nameUUIDFromBytes(byte[] name): Static factory to retrieve a type 3 (name based) UUID based on the specified byte array.
Syntax: public static UUID nameUUIDFromBytes(byte[] name)
Returns: A UUID generated from the specified array.
Exception: NA.
Java
// Java code illustrating nameUUIDFromBytes() methodsimport java.util.UUID;class UUIDdemo{    public static void main(String arg[]) throws         UnsupportedOperationException    {        // creating byte array        byte[] b = {10, 23, 45};                 // creating UUID from array        UUID gfg = UUID.nameUUIDFromBytes(b);                 // checking UUID        System.out.println(gfg);    }}
Output: 
f76a74ae-83b6-389c-82ca-8ac0b9febd33
long node(): The node value associated with this UUID. The 48 bit node value is constructed from the node field of this UUID. This field is intended to hold the IEEE 802 address of the machine that generated this UUID to guarantee spatial uniqueness. The node value is only meaningful in a time-based UUID, which has version type 1. If this UUID is not a time-based UUID then this method throws UnsupportedOperationException.
Syntax: public long node().
Returns: The node value of this UUID.
Exception: 
UnsupportedOperationException - If this UUID is not a version 1 UUID
Java
// Java code illustrating node() methodimport java.util.UUID;class UUIDdemo{    public static void main(String arg[])     {        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");                 // checking node value for this UUID        System.out.println("Node value: "          + gfg.node());    }}
Output: 
Node value: 138509024482011
static UUID randomUUID(): Static factory to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.
Syntax: public static UUID randomUUID().
Returns: randomly generated UUID.
Exception: NA
Java
// Java code illustrating randomUUID() methodimport java.util.UUID;class UUIDdemo{    public static void main(String arg[]) throws            UnsupportedOperationException    {        UUID gfg = UUID.randomUUID();                 // checking  this UUID        System.out.println("UUID: "          + gfg);    }}
Output: 
UUID: 937418f1-f1b6-4f7a-b9f6-9fa51ba780e3
long timestamp(): The timestamp value associated with this UUID. The 60 bit timestamp value is constructed from the time_low, time_mid, and time_hi fields of this UUID. The resulting timestamp is measured in 100-nanosecond units since midnight, October 15, 1582 UTC. The timestamp value is only meaningful in a time-based UUID, which has version type 1. If this UUID is not a time-based UUID then this method throws UnsupportedOperationException.
Syntax: public long timeStamp().
Returns: the time stamp value.
Exception: NA.
Java
// Java code illustrating timeStamp() methodimport java.util.UUID;class UUIDdemo{    public static void main(String arg[]) throws            UnsupportedOperationException    {        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");                 // checking time stamp for this UUID        System.out.println("time stamp: "          + gfg.timestamp());    }}
Output: 
time stamp: 137004589606850094
String toString(): This method returns a String object representing this UUID.
Syntax: public String toString().
Returns: a string object for this UUID.
Exception: NA
Java
// Java code illustrating toString methodimport java.util.UUID;class UUIDdemo{    public static void main(String arg[]) throws            UnsupportedOperationException    {        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");                 // checking string format for this UUID        System.out.println("String equivalent: "          + gfg.toString());    }}
Output: 
String equivalent: c81d4e2e-bcf2-11e6-869b-7df92533d2db
int variant(): The variant number associated with this UUID. The variant number describes the layout of the UUID.
Syntax: public int variant()
Returns: The variant number of this UUID
Exception: NA
Java
// Java code illustrating variant() methodimport java.util.UUID;class UUIDdemo{    public static void main(String arg[]) throws            UnsupportedOperationException    {        UUID gfg = UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db");                 // checking variant number for this UUID        System.out.println("variant number is: "          + gfg.variant());    }}
Output: 
variant number is: 2
int version(): The version number associated with this UUID. The version number describes how this UUID was generated. The version number has the following meaning: 1 Time-based UUID2 DCE security UUID3 Name-based UUID4 Randomly generated UUID
1 Time-based UUID
2 DCE security UUID
3 Name-based UUID
4 Randomly generated UUID
This article is contributed by Abhishek Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
prachisoda1234
germanshephered48
Java - util package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.