This is a very common scenario in programming when there is a requirement to use some unique random id. This tutorial will help us to generate it in Java. In this post, we will see “how to create a unique random ID using the GUID algorithm?”

Simplest example of generating a unique random id using GUID algorithm in Java...!!! Share on X

Example

import java.util.UUID;

public class GenerateUniqueIDUsingGUID {
	public static void main(String[] args) {
		System.out.println("Unique ID : "+generateUniqueID());
	}

	public static String generateUniqueID() {
		String randomUUID = "";
		try {
			UUID uuid = UUID.randomUUID();
			randomUUID = uuid.toString();
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		return randomUUID;
	}
}

 

Output:

Unique ID : 7833b8e9-f536-437d-98a9-e7facc330f8c

 

Simplest example of generating a unique random id using GUID algorithm in Java...!!! Share on X

 

If you like this post, please click like button and share it with others on Twitter. Also, check out my other useful blog posts on Java:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts