Generate unique identifier or UUID or GUID in java (with example)

  • Universal unique identifier (UUID) or Globally unique identifier (GUID) is unique identifier (or Id).
    • GUID represents the unique id(s).
  • Typically UUID is used in following scenarios:
    • UUID is used as a primary key in database
    • Window’s registry is using GUID (or UUID) to uniquely identify (installed) resources.
    • GUID is also used to generate unique name for IO resources like file name etc.
  • We will generate UUID using java.util.UUID class.

Program: generate unique identifier or UUID or GUID in java

package org.learn.uuid;

import java.util.UUID;

public class GenerateUUID {
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        System.out.printf("1. Generated GUID or UUID is : %s", uuid.toString());
        uuid = UUID.randomUUID();
        System.out.printf("\n2. Generated another GUID or UUID : %s", uuid.toString());
    }
}

Output: unique identifier or UUID or GUID in java

1. Generated GUID or UUID is : f9efc66d-7727-4ec9-b628-88f1cab314e8
2. Generated another GUID or UUID : 15a606d5-ea51-4cd9-8342-53f9c6d1c81b
Scroll to Top