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.
- 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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[]) throws
UnsupportedOperationException
{
UUID gfg = UUID.fromString( "c81d4e2e-bcf2-11e6-869b-7df92533d2db" );
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[])
{
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[])
{
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[])
{
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[])
{
UUID gfg = UUID.randomUUID();
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[])
{
UUID gfg = UUID.randomUUID();
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[])
{
UUID gfg = UUID.randomUUID();
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[]) throws
UnsupportedOperationException
{
byte [] b = { 10 , 23 , 45 };
UUID gfg = UUID.nameUUIDFromBytes(b);
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[])
{
UUID gfg = UUID.fromString( "c81d4e2e-bcf2-11e6-869b-7df92533d2db" );
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[]) throws
UnsupportedOperationException
{
UUID gfg = UUID.randomUUID();
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[]) throws
UnsupportedOperationException
{
UUID gfg = UUID.fromString( "c81d4e2e-bcf2-11e6-869b-7df92533d2db" );
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[]) throws
UnsupportedOperationException
{
UUID gfg = UUID.fromString( "c81d4e2e-bcf2-11e6-869b-7df92533d2db" );
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
import java.util.UUID;
class UUIDdemo
{
public static void main(String arg[]) throws
UnsupportedOperationException
{
UUID gfg = UUID.fromString( "c81d4e2e-bcf2-11e6-869b-7df92533d2db" );
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 UUID
- 2 DCE security UUID
- 3 Name-based UUID
- 4 Randomly generated UUID
Last Updated :
19 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...