Java Generate GUID

How to generate UUID or GUID in java?

To generate UUID in Java we use the UUID class present in java.util package. The UUID.randomUUID() method of this class returns a UUID object. To extract the value of the random string generated from the object we need to call toString() method on UUID object.

import java.util.UUID;

public class UUIDString {
  public static void main(String[] args) {

	UUID uuid1 = UUID.randomUUID();
	UUID uuid2 = UUID.randomUUID();
	String randomUUIDString1 = uuid1.toString();
	String randomUUIDString2 = uuid2.toString();

	System.out.println("UUID version       = " + uuid1.version());
	System.out.println("UUID variant       = " + uuid1.variant());
	System.out.println("First Random UUID String = " + randomUUIDString1);
	System.out.println("Second Random UUID String = " + randomUUIDString2);

Output of Program:

UUID version       = 4
UUID variant       = 2
First Random UUID String = c7d0b6b3-f5a0-4ba0-8a71-dc0dd4872496
Second Random UUID String = 84c175b8-2eec-4ad9-81b6-9428814f7a98
Further if you want to transmit this unique number over HTTP with dashes removed, you can simple use replace() method.
System.out.println("First Random UUID String = " + randomUUIDString1.replaceAll("-",""));

Output of above code:



Definition: Universally unique identifier (UUID) is a 128-bit number used to uniquely identify some object or entity on internet or information in computer systems is known as universally unique identifier (UUID). In some software it also referred to as globally unique identifier (GUID), software typically made by Microsoft.

Java UUID Collision

You might be thinking that does UUIDs are really unique? Has there been ever a UUID collision? How big is chance that a UUID collision will happen?

Although UUID is unique or at least it is extremely likely to be different from any other UUID generated until 3400 A.D. depending on the mechanism used to generate it, the probability that a UUID will be duplicated is not zero, it is close zero to be negligible.

Well, According to wikipedia, the probability that a random UUID will get duplicated is as below:

Only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. Or, to put it another way, the probability of one duplicate would be about 50% if every person on earth owned 600 million UUIDs.

The same logic applies to java UUIDs. So there is no need to worry about there collision unless there are some bugs / problems in underlying software.

Java UUID Format

A java UUID is composed of 16 octets which are represented as 32 hexadecimal digits, displayed in 5 groups separated by hyphens, in the form 8-4-4-4-12 which makes length of java UUID equal to 36 characters (32 alphanumeric characters and 4 hyphens) in its canonical textual form.

For example:
The 4 bits of digit M indicate the UUID version, and the 1–3 most significant bits of digit N indicate the UUID variant. In this example, M is 4, and N is 8, meaning that the UUID is a variant-2, version-4 UUID.

java.util.UUID class can be used to generate UUID in java. It was introduced in JDK 1.5. The UUID.randomUUID() method returns a UUID object. To get the value of UUID generated as string we need to call the toString() method on the UUID object returned.

To get the version and variant of the UUID we need to use version() and variant() methods respectively.

Java UUID Generator Online

To generate a UUID online just copy above java code to Generate UUID in Java and compile it using any online compiler like Jdoodle and you will get the UUID as output, simple :).

Applications of Java UUID
UUID are often used in programming for creating random names, transactions id. UUID can also be used as primary key in database.