Skip to content
Related Articles

Related Articles

Java.util.UUID class in Java
  • Last Updated : 08 Jul, 2017

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:

  1. 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 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
    
  2. 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 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
    
  3. 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 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
    
  4. 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 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
    
  5. 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 code illustrating getLeastSignificantBits() method
    import java.util.UUID;
    class UUIDdemo
    {
        public static void main(String arg[]) 
        {
            UUID gfg = UUID.randomUUID();
              
            // checking the least significatnt bit
            System.out.println("Least significant bit "
                    gfg.getLeastSignificantBits());
        }
    }

    Output:



    Least significant bit -8406445530268383532
    
  6. 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 code illustrating getMostSignificantBits() bit
    import java.util.UUID;
    class UUIDdemo
    {
        public static void main(String arg[]) 
        {
            UUID gfg = UUID.randomUUID();
              
            // checking the most significatnt bit
            System.out.println("Most significant bit "
                    gfg.getMostSignificantBits());
        }
    }

    Output:

    Most significant bit 8138958362250724568
    
  7. int hashCode(): this method returns hash code for this UUID.
    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
    
  8. 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 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
    
  9. 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 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
    
  10. 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 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
    
  11. 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 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 satmp for this UUID
            System.out.println("time stamp: "
              + gfg.timestamp());
        }
    }

    Output:

    time stamp: 137004589606850094
    
  12. 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 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
    
  13. 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 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
    
  14. 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 UUID
    • 2 DCE security UUID
    • 3 Name-based UUID
    • 4 Randomly generated UUID
    Syntax: public int version()
    Returns: the version number of this UUID.
    Exception: NA
    




    // Java code illustrating version() 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 version number for this UUID
            System.out.println("version number is: "
              + gfg.version());
        }
    }

    Output:

    version number is: 1
    

This article is contributed by Abhishek Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :