UUID
About
The UUID
(Universally Unique Identifier) class in Java provides a way to generate unique identifiers. It is part of the java.util
package and is commonly used in applications where globally unique IDs are required, such as in distributed systems, database keys, or unique identifiers for entities.
A UUID is a 128-bit value that is highly likely to be unique across all systems and contexts.
It adheres to the RFC 4122 standard, ensuring consistency and compatibility with other systems that use UUIDs.
UUIDs are represented as a 36-character string in hexadecimal format, separated into five groups:
8-4-4-4-12
(e.g.,550e8400-e29b-41d4-a716-446655440000
).Common use cases include identifying resources in a distributed environment, assigning unique database keys, and tagging data entities.
Features
Global Uniqueness:
Each UUID is designed to be unique, even if generated on different machines or at different times.
Different Types of UUIDs:
Version 1: Based on timestamp and MAC address.
Version 3: Based on a namespace and a name, using MD5 hashing.
Version 4: Randomly generated UUIDs.
Version 5: Based on a namespace and a name, using SHA-1 hashing.
Compact Representation:
Despite their uniqueness, UUIDs are compactly represented as a string or a byte array.
Thread-Safe:
UUID generation methods are thread-safe and can be safely used in multi-threaded environments.
Wide Compatibility:
UUIDs are widely used across programming languages, databases, and distributed systems.
Declaration
The UUID
class is part of the java.util
package. To use it, you simply import the class:
Methods Available
UUID Generation:
UUID.randomUUID()
: Generates a random UUID (Version 4).UUID.nameUUIDFromBytes(byte[] name)
: Generates a UUID based on the specified byte array (Version 3 or 5).
Get UUID Components:
long getMostSignificantBits()
: Returns the most significant 64 bits of the UUID.long getLeastSignificantBits()
: Returns the least significant 64 bits of the UUID.
UUID Parsing:
UUID.fromString(String uuid)
: Creates a UUID from its string representation.
Utility Methods:
int version()
: Returns the version number of the UUID.int variant()
: Returns the variant number of the UUID.String toString()
: Converts the UUID to its standard string representation.boolean equals(Object obj)
: Checks if two UUIDs are equal.int hashCode()
: Returns the hash code of the UUID.
Usage
Basic UUID Generation
Parsing a UUID String
Generating UUID from Namespace and Name
Getting UUID Components
Using UUID as a Database Key
Applications and Real-World Usage
Database Keys: UUIDs are often used as primary keys in distributed databases to avoid key collisions.
Distributed Systems: Used to identify resources, nodes, or messages uniquely in a network of distributed systems.
File or Data Tagging: UUIDs are assigned to files or records for tracking and identification purposes.
Session and Token Generation: UUIDs can be used to generate unique session IDs or authentication tokens.
Debugging and Logging: UUIDs provide a way to uniquely identify and track events or requests in logs.
Last updated
Was this helpful?