generate uuid java code example

Example 1: java random uuid

String id = UUID.randomUUID().toString();

Example 2: java uuid from string

// Import Java UUID Utils
import java.utils.UUID;

// Execute function fromString() with your string as argument.
// Returns an uuid, which is your string converted into an UUID.
UUID uuid = UUID.fromString(string);

Example 3: java uuid

import java.util.UUID;

public class RandomStringUUID {
    public static void main(String[] args) {
        // Creating a random UUID (Universally unique identifier).
        UUID uuid = UUID.randomUUID();
        String randomUUIDString = uuid.toString();

        System.out.println("Random UUID String = " + randomUUIDString);
        System.out.println("UUID version       = " + uuid.version());
        System.out.println("UUID variant       = " + uuid.variant());
    }
}

Example 4: how to generate random id in java

import java.util.UUID;
public class UUIDExample.
{
public static void main(String[] args)
{
UUID uuid=UUID.randomUUID(); //Generates random UUID.
System.out.println(uuid);
}
}

Tags:

Java Example