How do we generate UUID in Java?

Development & UX Researches
1 min readOct 13, 2020

--

Question

How do we generate UUID in Java?

UUID aka — Universally Unique Identifier or GUID aka — GUID Globally Unique Identifier. The id is a 128-bit long value and uses hex digits (octets):

9d980a44–7e4e-4461-ac92-a91d6dc0a68b

A UUID or GUID can be used in programming while assigning a unique id for a user, customer, employee, document, or any other purposes.

For instance, When I had a postman collection that required a customer GUID and it had to be 128-bit long, but not just a random number or string, I came up with UUID util class which is an implementation of Java.

How do we use it?

* Import the UUID Java util class
* Create the object of UUID shown as below code snippet.

Either you are using a logger, assigning to a value container, or just printing with a ‘sout’ to the console, you are free of picking your choice. Here I want to use is a quick ‘sout’ to show how it works. We can write the UUID inside a ‘main method’ or use Junit / TestNG Annotations to make it happen faster on the console of your IDE.

//Import the UUID Java util class;
import java.util.UUID; //Create the object of UUID;
UUID id = UUID.randomUUID(); @Test
public void retrieveUniqueId(){
UUID id = UUID.randomUUID();
System.out.println(id);
}
public static void main(String[] args) {
UUID id = UUID.randomUUID();
System.out.println(id);
}

Happy coding!

--

--

Development & UX Researches

A self-learner in the programming and UX industry. Keep yourself updated in this journey together ;)