Open In App

Java.util.UUID class in Java

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. 

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: 



Syntax: public int clockSequence().
Returns: The clock sequence of this UUID.
Exception: 
UnsupportedOperationException - If this UUID is not a version 1 UUID




// Java code illustrating clockSequence() method
import 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
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 code illustrating compareTo() method
import 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
Syntax: public boolean equals(Object obj).
Returns: true if the objects are the same; false otherwise
Exception: NA.




// Java code illustrating equals() method
import 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
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 code illustrating fromString() method
import 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
Syntax: public long getLeastSignificantBits().
Returns: The least significant 64 bits of this UUID's 128 bit value.
Exception: NA.




// Java code illustrating getLeastSignificantBits() method
import 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
Syntax: public long getMostSignificantBits()
Returns: The most significant 64 bits of this UUID's 128 bit value.
Exception: NA




// Java code illustrating getMostSignificantBits() bit
import 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
Syntax: public int hashCode().
Returns: the hash code for this UUID.
Exception: NA.




// Java code illustrating hashCode method
import 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
Syntax: public static UUID nameUUIDFromBytes(byte[] name)
Returns: A UUID generated from the specified array.
Exception: NA.




// Java code illustrating nameUUIDFromBytes() methods
import 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
Syntax: public long node().
Returns: The node value of this UUID.
Exception: 
UnsupportedOperationException - If this UUID is not a version 1 UUID




// Java code illustrating node() method
import 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
Syntax: public static UUID randomUUID().
Returns: randomly generated UUID.
Exception: NA




// Java code illustrating randomUUID() method
import 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
Syntax: public long timeStamp().
Returns: the time stamp value.
Exception: NA.




// Java code illustrating timeStamp() method
import 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
Syntax: public String toString().
Returns: a string object for this UUID.
Exception: NA




// Java code illustrating toString method
import 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
Syntax: public int variant()
Returns: The variant number of this UUID
Exception: NA




// Java code illustrating variant() method
import 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

Article Tags :