Home » Java UUID

Java UUID

What is UUID?

UUID stands for Universally Unique Identifier. UUID are standardized by the Open Software Foundation (OSF). It is a part of Distributive Computing Environment (DCE). A UUID is 36 characters long unique number. It is also known as a Globally Unique Identifier (GUID).

Java UUID Class

  • A UUID is a class that represents an immutable Universally Unique Identifier (UUID).
  • A UUID represents a 128-bit long value that is unique to all practical purpose. It is used to identify information in the computer system.
  • The UUID class belongs to java.util package.
  • The UUID class implements Serializable and Comparable interface and extends Object class.
  • There exist many variants of these global identifiers.
  • The methods of the UUID class are for manipulating the Leach-Salz variant.

Java UUID Representation

The representation of the UUID uses hex digits.

Java UUID is made up of hex digit along with four hyphens (-). It is 36 characters long unique number, including four hyphens. A UUID may be nil, in which all bits are set to zero.

Java UUID

Name Length (Hex Digits) Length (bytes) Contents
time_low 8 4 low 32-bits of the time
time_mid 4 2 middle 16-bits of the time
time_hi and_version 4 2 4-bit version in the MSB, followed by the high 12-bits of the time
clock_seq_hi_and_res clock_seq_low 4 2 1-3 bit variant in the most significant bits followed by the 13-15 bit clock sequence
node 12 6 48-bit node ID

Java UUID Usage

It is used to create the following:

  • Session ID for web application
  • Transaction ID
  • Random file name
  • The primary key for database table

Leach-Salz variant: The variant field contain a value that identifies the layout of the UUID. The representation of a variant 2 (Leach-Salz) UUID is as follows:

  • 0xFFFFFFFF00000000 (indicates time_low)
  • 0X0000000FFFF0000 (indicates time_mid)
  • 0x0000000000000FF (indicates time_hi)
  • 0x00000000000F000 (version)

The bit layout defined above is valid only for a UUID with a variant value of 2 that indicates the Leach-Salz variant.

Types of UUID

There are four types of UUIDs:

  • time-based UUID (version 1)
  • DCE security UUID (version 2)
  • name-based UUID (version 3 and 5)
  • randomly generated UUID (version 4)

Version 3 and 5

The UUID version 3 and 5 generate the UUID using the hash of namespace and name. The namespace identifiers are UUIDs like Domain Name System (DNS), URLs, and OIDs (Object Identifiers), etc. The difference between UUID version 3 and UUID version 5 is of the hashing algorithm.

Version 3 uses MD5 (128-bits) algorithm, while version 5 uses SHA-1 (160-bits) algorithm. We trim the resulting hash to 128-bits and then replace 4 bit for the version and 2 bit for the variant. Java does not provide the implementation of version 5.

Version 4

The UUID version 4 implementation uses random numbers as the source. It uses an unpredictable value as the seed to generate a random number to reduce the chance of collisions.


UUID Variants

The most used variant is 2 (Leach-Salz). There are the following types of variants:

  • 0: It is reserved for NCS backward compatibility
  • 2: Leach-Salz
  • 6: It is reserved for Microsoft backward compatibility
  • 7: It is reserved for future definition.

Java UUID 2

In the above figure “V” indicates the UUID version and the “R” (1-3) indicate the UUID variant.

Java UUID Constructor

We can generate different types of UUID by using constructor. The syntax of the constructor is:

The argument mostSigBits is used for the MSB (64-bit) of the UUID and leastSigBits becomes LSB (64-bit) of the UUID.


Methods of UUID class

UUID ramdomUUID() method

The randomUUID() method randomly generate the UUID. Whenever we run the program, it generates a new UUID. The signature of the method is:

The method returns the randomly generated UUID.

Example

The following example generates a random UUID.

Output

c6a8669e-ee95-4c42-9ef6-4a9b61380164 

UUID version() method

The version associated with this UUID. The version number describe how this UUID was generated. The method returns the version number of the UUID. The signature of the method is:

Example

Output

UUID version is: 1 UUID version is: 3 

UUID variant() method

The variant number associated with this UUID. It describes the layout of the UUID. The method returns the variant number of the UUID. The signature of the method is:

Example

Output

UUID variant is: 2 UUID variant is: 7 

UUID node() method

The node() method returns the node value associated with the UUID. It is a 48-bit node value. It is constructed from the field of this UUID. It holds the IEEE 802 address (MAC Address) of the machine. It throws the UnsupportedOperationException if the UUID is not a version 1 UUID.

The signature of the method is:

Example

Output

Node value: 55075465998336 

Next Topic

You may also like