Skip to content

Most visited

Recently visited

navigation
Added in API level 1

UUID

public final class UUID
extends Object implements Serializable, Comparable<UUID>

java.lang.Object
   ↳ java.util.UUID


UUID is an immutable representation of a 128-bit universally unique identifier (UUID).

There are multiple, variant layouts of UUIDs, but this class is based upon variant 2 of RFC 4122, the Leach-Salz variant. This class can be used to model alternate variants, but most of the methods will be unsupported in those cases; see each method for details.

Summary

Public constructors

UUID(long mostSigBits, long leastSigBits)

Constructs an instance with the specified bits.

Public methods

int clockSequence()

The clock sequence value of the version 1, variant 2 UUID as per RFC 4122.

int compareTo(UUID uuid)

Compares this UUID to the specified UUID.

boolean equals(Object object)

Compares this UUID to another object for equality.

static UUID fromString(String uuid)

Parses a UUID string with the format defined by toString().

long getLeastSignificantBits()

The 64 least significant bits of the UUID.

long getMostSignificantBits()

The 64 most significant bits of the UUID.

int hashCode()

Returns a hash value for this UUID that is consistent with the equals(Object) method.

static UUID nameUUIDFromBytes(byte[] name)

Generates a variant 2, version 3 (name-based, MD5-hashed) UUID as per RFC 4122.

long node()

The node value of the version 1, variant 2 UUID as per RFC 4122.

static UUID randomUUID()

Generates a variant 2, version 4 (randomly generated number) UUID as per RFC 4122.

long timestamp()

The timestamp value of the version 1, variant 2 UUID as per RFC 4122.

String toString()

Returns a string representation of this UUID in the following format, as per RFC 4122.

int variant()

The variant of the UUID as per RFC 4122.

int version()

The version of the variant 2 UUID as per RFC 4122.

Inherited methods

From class java.lang.Object
From interface java.lang.Comparable

Public constructors

UUID

Added in API level 1
UUID (long mostSigBits, 
                long leastSigBits)

Constructs an instance with the specified bits.

Parameters
mostSigBits long: The 64 most significant bits of the UUID.
leastSigBits long: The 64 least significant bits of the UUID.

Public methods

clockSequence

Added in API level 1
int clockSequence ()

The clock sequence value of the version 1, variant 2 UUID as per RFC 4122.

Returns
int a long value.
Throws
UnsupportedOperationException if version() is not 1.

compareTo

Added in API level 1
int compareTo (UUID uuid)

Compares this UUID to the specified UUID. The natural ordering of UUIDs is based upon the value of the bits from most significant to least significant.

Parameters
uuid UUID: the UUID to compare to.
Returns
int a value of -1, 0 or 1 if this UUID is less than, equal to or greater than uuid.

equals

Added in API level 1
boolean equals (Object object)

Compares this UUID to another object for equality. If object is not null, is a UUID instance, and all bits are equal, then true is returned.

Parameters
object Object: the Object to compare to.
Returns
boolean true if this UUID is equal to object or false if not.

fromString

Added in API level 1
UUID fromString (String uuid)

Parses a UUID string with the format defined by toString().

Parameters
uuid String: the UUID string to parse.
Returns
UUID an UUID instance.
Throws
NullPointerException if uuid is null.
IllegalArgumentException if uuid is not formatted correctly.

getLeastSignificantBits

Added in API level 1
long getLeastSignificantBits ()

The 64 least significant bits of the UUID.

Returns
long the 64 least significant bits.

getMostSignificantBits

Added in API level 1
long getMostSignificantBits ()

The 64 most significant bits of the UUID.

Returns
long the 64 most significant bits.

hashCode

Added in API level 1
int hashCode ()

Returns a hash value for this UUID that is consistent with the equals(Object) method.

Returns
int an int value.

nameUUIDFromBytes

Added in API level 1
UUID nameUUIDFromBytes (byte[] name)

Generates a variant 2, version 3 (name-based, MD5-hashed) UUID as per RFC 4122.

Parameters
name byte: the name used as byte array to create an UUID.
Returns
UUID an UUID instance.

node

Added in API level 1
long node ()

The node value of the version 1, variant 2 UUID as per RFC 4122.

Returns
long a long value.
Throws
UnsupportedOperationException if version() is not 1.

randomUUID

Added in API level 1
UUID randomUUID ()

Generates a variant 2, version 4 (randomly generated number) UUID as per RFC 4122.

Returns
UUID an UUID instance.

timestamp

Added in API level 1
long timestamp ()

The timestamp value of the version 1, variant 2 UUID as per RFC 4122.

Returns
long a long value.
Throws
UnsupportedOperationException if version() is not 1.

toString

Added in API level 1
String toString ()

Returns a string representation of this UUID in the following format, as per RFC 4122.

            UUID                   = time-low "-" time-mid "-"
                                     time-high-and-version "-"
                                     clock-seq-and-reserved
                                     clock-seq-low "-" node
            time-low               = 4hexOctet
            time-mid               = 2hexOctet
            time-high-and-version  = 2hexOctet
            clock-seq-and-reserved = hexOctet
            clock-seq-low          = hexOctet
            node                   = 6hexOctet
            hexOctet               = hexDigit hexDigit
            hexDigit =
                "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /
                "a" / "b" / "c" / "d" / "e" / "f" /
                "A" / "B" / "C" / "D" / "E" / "F"
 

Returns
String a String instance.

variant

Added in API level 1
int variant ()

The variant of the UUID as per RFC 4122.

  • 0 - Reserved for NCS compatibility
  • 2 - RFC 4122/Leach-Salz
  • 6 - Reserved for Microsoft Corporation compatibility
  • 7 - Reserved for future use

Returns
int an int value.

version

Added in API level 1
int version ()

The version of the variant 2 UUID as per RFC 4122. If the variant is not 2, then the version will be 0.

  • 1 - Time-based UUID
  • 2 - DCE Security UUID
  • 3 - Name-based with MD5 hashing UUID (nameUUIDFromBytes(byte[]))
  • 4 - Randomly generated UUID (randomUUID())
  • 5 - Name-based with SHA-1 hashing UUID

Returns
int an int value.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.