java guid

java guid

A globally unique identifier (GUID) is a string that is used to uniquely identify an object or entity. GUIDs are often used to identify objects in software systems, such as database records, files, or network resources.

In Java, you can use the UUID class of the java.util package to generate and work with GUIDs.

Here is an example of how to generate a random GUID in Java:

UUID uuid = UUID.randomUUID();
String guid = uuid.toString();
Sour‮al.www:ec‬utturi.com

The randomUUID method generates a random UUID, and the toString method returns it as a string. The string representation of a UUID is in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where "x" is a hexadecimal digit.

You can also generate a UUID from a namespace and name using the nameUUIDFromBytes method:

String namespace = "https://example.com/";
String name = "object-name";
UUID uuid = UUID.nameUUIDFromBytes((namespace + name).getBytes(StandardCharsets.UTF_8));
String guid = uuid.toString();

In this example, the uuid variable will contain a UUID that is generated based on the namespace and name.

You can use the UUID class to compare, hash, and format UUIDs as well.

For example:

UUID uuid1 = UUID.fromString("00000000-0000-0000-0000-000000000000");
UUID uuid2 = UUID.fromString("00000000-0000-0000-0000-000000000000");
if (uuid1.equals(uuid2)) {
    // the UUIDs are equal
}
int hash = uuid1.hashCode();
String str = uuid1.toString();

Note that UUIDs are generally considered to be unique, but there is a small chance of collision (two different objects having the same UUID). In practice, the probability of collision is very low and can be ignored for most purposes.

Created Time:2017-11-03 22:21:12  Author:lautturi