Universally Unique Identifier (UUID) is a 128-bit Identifier used in computer systems to identify the number of pieces of information, and a related term: global Unique Identifier (GUID).
Unlike most other numbering schemes, UUID is unique, generated from a standard approach that does not rely on registration and assignment by a central authority. The probability of repeating the UUID code is close to zero and can be ignored.
UUID is a set of 32-bit hexadecimal numbers, so the theoretical total number of UUID is 16^32=2^128, which is approximately 3.4 x 10^38. That is, if 1 trillion UUID is generated every nanosecond (ns), it would take 10 billion years to run out of UUID.
The standard form of UUID consists of 32 hexadecimal digits, hyphenated into five paragraphs of the form 8-4-4-12.
Example: e8400 e29b – 550-41 d4 – a716-446655440000
UUID format:
In its canonical textual representation, the 16 8-bit bytes of the UUID are represented as 32 hexadecimal (base 16) digits, displayed in five groups separated by a hyphen ‘-‘, “8-4-4-12” for a total of 36 characters (32 alphanumeric characters and 4 hyphens). Such as:
123e4567-e89b-12d3-a456-426655440000
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
Copy the code
Ps: A indicates 10 in hexadecimal notation
The four-digit M represents the UUID version, and the one to three most significant bits of the N represent the UUID variant. In the example, M is 1 and N is a (10xx), which means that this UUID is a “variant 1”, “version 1” UUID; The DCE/RFC 4122 UUID is based on time.
UUID version:
For “variants 1” and “Variants 2”, there are five versions defined in the standard and each may be more appropriate than the others for a particular use case.
The version is indicated by the M string.
Version 1-Uuid is generated based on time and node ID (usually MAC address);
Version 2-Uuid is generated from an identifier (usually a group or user ID), time, and node ID;
Version 3, version 5 – Deterministic UUID is generated through hashing namespace identifiers and names;
Version 4 – UUID is generated using randomness or pseudo-randomness.
Generate UUID in Java:
Let’s look at the methods in the Java UUID class that can be used to generate UUID:
1. randomUUID():
It uses a high encryption strength pseudorandom number generator to generate a V4 pseudorandom UUID:
UUID uuid = UUID.randomUUID();
2. nameUUIDFromBytes():
We can use nameuidFromBytes () to generate UUID frombyte array:
byte[] byteArr = {11, 23, 33};
UUID uuid = UUID.nameUUIDFromBytes(byteArr);
This method generates v3 UUID (name-based).
3. fromString():
Using fromString (), we can create UUID from a standard string representation:
UUID uuid = UUID.fromString(
"533a4559-e55c-18b3-2456-555563322002"
);
It throws an IllegalArgumentException for any invalid string passed in as an argument.
Compare two UUids:
The Java UUID implements the Comparable interface, so we can use the compareTo () method to compare them
UUID uuid1 = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();
int` `result = uuid1.compareTo(uuid2);
Copy the code
As we know, the compareTo () method returns:
- 1: if uuid1 is greater than uuid2
- Zero: if uuid1 = uuid2
- -1: If uuid1 is smaller than uuid2
We can also choose to use the equals () method for comparison.
Conclusion:
In this tutorial, you learned what a UUID is and its format and version. We also showed you how to generate UUids from Java UUID classes.
About the author
Hello everyone, MY name is Uncle Mai, currently engaged in Java development, front-end and back-end a little understanding. Hope to like my partner to pay attention to my personal public number, convenient access to first-hand article information. Of course, you can also add my personal wechat (right below), we make a friend, thank you for your support to me.