Java UUID Class

Introduction

The UUID class in Java, part of the java.util package, is used to generate and manipulate Universally Unique Identifiers (UUIDs). A UUID is a 128-bit value used for uniquely identifying information in distributed systems without significant central coordination. The UUID class provides methods to create, convert, and work with UUIDs.

Table of Contents

  1. What is the UUID Class?
  2. Common Methods
  3. Examples of Using the UUID Class
  4. Conclusion

1. What is the UUID Class?

The UUID class represents an immutable universally unique identifier (UUID). A UUID is a 128-bit value consisting of 32 hexadecimal digits, displayed in five groups separated by hyphens (8-4-4-4-12).

2. Common Methods

  • randomUUID(): Generates a random UUID.
  • nameUUIDFromBytes(byte[] name): Generates a UUID based on the specified byte array.
  • fromString(String name): Creates a UUID from the string standard representation.
  • toString(): Returns a string representation of the UUID.
  • getLeastSignificantBits(): Returns the least significant 64 bits of the UUID.
  • getMostSignificantBits(): Returns the most significant 64 bits of the UUID.
  • version(): Returns the version number associated with the UUID.
  • variant(): Returns the variant number associated with the UUID.
  • compareTo(UUID val): Compares this UUID with the specified UUID.

3. Examples of Using the UUID Class

Example 1: Generating a Random UUID

This example demonstrates how to generate a random UUID.

import java.util.UUID;

public class RandomUUIDExample {
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        System.out.println("Generated UUID: " + uuid.toString());
    }
}

Output:

Generated UUID: a7a230d2-1cee-43aa-8a63-3a903603f4d5

Example 2: Generating a UUID from a Name

This example shows how to generate a UUID based on a byte array, which can be derived from a string name.

import java.util.UUID;

public class NameUUIDExample {
    public static void main(String[] args) {
        String name = "example.com";
        UUID uuid = UUID.nameUUIDFromBytes(name.getBytes());
        System.out.println("Generated UUID from name: " + uuid.toString());
    }
}

Output:

Generated UUID from name: 5ababd60-3b22-3803-82dd-8d83498e5172

Example 3: Creating a UUID from a String

This example demonstrates how to create a UUID from its string representation.

import java.util.UUID;

public class UUIDFromStringExample {
    public static void main(String[] args) {
        String uuidString = "123e4567-e89b-12d3-a456-426614174000";
        UUID uuid = UUID.fromString(uuidString);
        System.out.println("UUID from string: " + uuid.toString());
    }
}

Output:

UUID from string: 123e4567-e89b-12d3-a456-426614174000

Example 4: Getting UUID Parts

This example shows how to get the most significant bits and least significant bits of a UUID.

import java.util.UUID;

public class UUIDPartsExample {
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        long mostSignificantBits = uuid.getMostSignificantBits();
        long leastSignificantBits = uuid.getLeastSignificantBits();

        System.out.println("UUID: " + uuid.toString());
        System.out.println("Most Significant Bits: " + mostSignificantBits);
        System.out.println("Least Significant Bits: " + leastSignificantBits);
    }
}

Output:

UUID: f69ea175-7eb2-4b1b-995c-9ede8896cce0
Most Significant Bits: -675925368050136293
Least Significant Bits: -7395861809437684512

Example 5: Comparing UUIDs

This example demonstrates how to compare two UUIDs.

import java.util.UUID;

public class CompareUUIDExample {
    public static void main(String[] args) {
        UUID uuid1 = UUID.randomUUID();
        UUID uuid2 = UUID.randomUUID();

        System.out.println("UUID 1: " + uuid1.toString());
        System.out.println("UUID 2: " + uuid2.toString());

        int comparison = uuid1.compareTo(uuid2);
        if (comparison < 0) {
            System.out.println("UUID 1 is less than UUID 2");
        } else if (comparison > 0) {
            System.out.println("UUID 1 is greater than UUID 2");
        } else {
            System.out.println("UUID 1 is equal to UUID 2");
        }
    }
}

Output:

UUID 1: a8f629ea-fa0b-4256-974c-ec7edb6e5ad0
UUID 2: 511cb9e7-d506-416a-b5e0-7d68a56f2651
UUID 1 is less than UUID 2

Example 6: UUID Version and Variant

This example shows how to get the version and variant of a UUID.

import java.util.UUID;

public class UUIDVersionVariantExample {
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        int version = uuid.version();
        int variant = uuid.variant();

        System.out.println("UUID: " + uuid.toString());
        System.out.println("Version: " + version);
        System.out.println("Variant: " + variant);
    }
}

Output:

UUID: dbdf39d0-3e0a-4def-8c04-986843ef75bb
Version: 4
Variant: 2

4. Conclusion

The UUID class in Java provides a convenient way to generate and manipulate universally unique identifiers. UUIDs are essential for ensuring unique identification in distributed systems and other applications where uniqueness is crucial. The examples provided demonstrate common usage patterns and highlight the capabilities of the UUID class, making it a valuable tool for generating unique identifiers in Java applications.

Comments