UUID stands for Universally Unique Identifier. UUIDs are used as IDs (to identify) unique objects or records. An easy way to generate UUIDs in Java is to use the java.util.UUID class. Different variants and variant-versions exist for UUID objects. The methods of this class generally manipulate the Leach-Salz variant, although the constructors allow the creation of any variant of UUID.

UUID objects have methods available that return information such as the UUID variant, version, and timestamp (timestamp only available for time-based UUID). Below is an example of creating a UUID and printing its hexadecimal string representation along with its variant and version.

Example Creating UUID in Java

package com.bigdatums.uuid;

import java.util.UUID;

public class GenerateUUIDExample {

    public static void main(String[] args) {
        //create random uuid object
        UUID randomUUID = UUID.randomUUID();

        //print uuid hexadecimal representation along with variant and version
        System.out.println(randomUUID);
        System.out.println(randomUUID.variant());
        System.out.println(randomUUID.version());
    }
}

For more information about UUID variants, versions, etc. the Wikipedia UUID Page is a great resource. A higher level explanation of UUIDs can also be found in the post What is a UUID?

Leave a Reply

How to Generate a UUID in Java