Create a GUID in Java
It depends what kind of UUID you want.
The standard Java
UUID
class generates Version 4 (random) UUIDs. (UPDATE - Version 3 (name) UUIDs can also be generated.) It can also handle other variants, though it cannot generate them. (In this case, "handle" means constructUUID
instances fromlong
,byte[]
orString
representations, and provide some appropriate accessors.)The Java UUID Generator (JUG) implementation purports to support "all 3 'official' types of UUID as defined by RFC-4122" ... though the RFC actually defines 4 types and mentions a 5th type.
For more information on UUID types and variants, there is a good summary in Wikipedia, and the gory details are in RFC 4122 and the other specifications.
java.util.UUID.randomUUID();
Have a look at the UUID class bundled with Java 5 and later.
For example:
- If you want a random UUID you can use the randomUUID method.
- If you want a UUID initialized to a specific value you can use the UUID constructor or the fromString method.
Just to extend Mark Byers's answer with an example:
import java.util.UUID;
public class RandomStringUUID {
public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
System.out.println("UUID=" + uuid.toString() );
}
}