How do we generate UUID in Java?

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!

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build and Release — Issue #8

Angular 6

Automated Hadoop, AWS, LVM and Docker using python

CDK — The write way to Infra Code

Stop copying Google

Implementing Speed

Towards Open Options Chains Part III: Getting Started with Airflow

WSO2 Hackathon — 2021

Php Grid Open Source For Mac

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sdet & Updates

Sdet & Updates

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

More from Medium

Head First Java Chapter 6 Summery

What is String in Java?

Head First Java — Chapter 3