CodeGym/Java Blog/Java Developer/Guide to UUID in Java
Author
Aditi Nawghare
Software Engineer at Siemens

Guide to UUID in Java

Published in the Java Developer group
members
UUID stands for Universally Unique Identifier, which is a 128-bit long value used for identifying information in computer systems. A UUID generated by the UUID generator is designed to be unique across all systems and all time. The length of a UUID is 36 characters, including hyphens, and is typically represented in hexadecimal notation.

What is UUID in Java?

The UUID class in Java is a part of the java.util package and provides methods for generating and manipulating UUIDs. It is a commonly used class in Java for generating unique identifiers. With the help of the UUID class in Java, we can easily generate random UUIDs, as well as create UUID from different sources such as byte arrays, strings, and long values. We can get a variety of different generated UUIDs each time the generator is called. These generated UUIDs are unique and are designed to be globally unique, meaning that the same UUID will not be generated twice by different systems. We can use generated UUIDs for a variety of purposes such as session IDs, database keys, and other scenarios where uniqueness is important.

Generating UUID in Java

To generate UUID in Java, we use the java.util.UUID class. The java.util.UUID class provides two methods for generating UUIDs: randomUUID() and nameUUIDFromBytes(byte[] name). The randomUUID() method generates a random UUID. This method does not require any arguments, and it generates a random UUID every time it is called. Here is an example of how to use the randomUUID() method:
import java.util.UUID;

public class UUIDExample {
    public static void main(String[] args) {
        // generating a random UUID
        UUID uuid = UUID.randomUUID();

        // print the UUID
        System.out.println("Generated UUID: " + uuid);
    }
}

Output

Generated UUID: d3ee2929-212b-4077-af84-694a0e69b8e1
The nameUUIDFromBytes(byte[] name) method generates a UUID from a byte array. This method takes a byte array as an argument and generates a UUID based on the contents of the byte array. Here is an example of how to use the nameUUIDFromBytes(byte[] name) method:
import java.util.UUID;

public class UUIDExample {
    public static void main(String[] args) {
        // generating a UUID from a byte array
        byte[] name = { 1, 2, 3, 4, 5 };
        UUID uuid = UUID.nameUUIDFromBytes(name);

        // print the UUID
        System.out.println("Generated UUID: " + uuid);
    }
}

Output

Generated UUID: 4d6579d4-c3e7-38a1-9246-017e903b2c33

UUID Properties

UUIDs have the following properties:
  • Length: UUIDs are 128 bits long, which means they contain 16 bytes of data. This length makes them suitable for use in computer systems as they can be stored efficiently.
  • Uniqueness: The probability of two UUIDs being the same is very low. The chance of a collision is about 1 in 2^128, which is an extremely small number.
  • Randomness: The UUID is generated using an algorithm that ensures randomness. This randomness makes it difficult for anyone to predict the UUIDs that will be generated.
  • Version: UUIDs are divided into versions, with each version using a different algorithm to generate the UUID. The most commonly used versions are version 1 and version 4. Version 1 UUIDs are generated using the current time and the MAC address of the computer that is generating the UUID. Version 4 UUIDs are generated using random numbers.

Length of a UUID

A UUID is a 128-bit value, represented by a string of 36 characters. The string contains 32 hexadecimal digits, divided into groups of 8, 4, 4, 4, and 12 digits, separated by hyphens. Here is an example of a UUID string:
123e4567-e89b-12d3-a456-426655440000
Example UUID: Here is an example UUID, generated using the randomUUID() method:
d3ee2929-212b-4077-af84-694a0e69b8e1
This UUID consists of 32 hexadecimal digits, divided into groups of 8, 4, 4, 4, and 12 digits, separated by hyphens. In the example UUID above when we talk about the UUID class in Java, the term generating the UUID refers to the process of creating a new UUID using the java.util.UUID class. This process involves calling one of the UUID class's static factory methods such as randomUUID() or nameUUIDFromBytes(byte[]). Once we call one of these methods, the UUID class in Java will create a new UUID and return it to us. These generated UUIDs can then be used in our application as needed. Guide to UUID in Java - 1

Conclusion

In this article, we discussed what UUIDs are and how to generate them using the java.util.UUID class in Java. We also looked at the properties of UUIDs, including their length, uniqueness, randomness, and version. UUIDs are widely used in computer systems to identify information in a unique way. They are useful in distributed systems and databases where multiple computers or nodes need to access and manipulate data. With the java.util.UUID class in Java, you can easily generate UUIDs in your Java projects.
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet