Generating a UUID (aka GUID) in Java

UUID stands for Universally Unique ID. A UUID is known in the Microsoft world as as a GUID.

What does a UUID look like?

The UUID is not a string, but a 128-bit value. The UUID is a hexadecimal value like

How to generate a UUID in Java?

The “java.util.UUID” class that represents an immutable universally unique identifier (UUID).

What are the practical use cases for UUID?
  1. As a row identity in a database table. i.e. as a primary key. The benefit is that it will be unique across every table, every database, and every server. You can generate IDs anywhere. The disadvantage is that it is 4 times larger than the traditional 4-byte index value. UUIDs may not index well and could degrade database performance.
  2. UUID is a natural fit for many development scenarios where you need to generate primary keys outside the database.
  3. Many data replication scenarios require UUID columns. Another option is to let the database generate a sequence key for internal relationships, and use a UUID as a unique secondary key.
  4. It can be used as an audit tracking id in web service calls.
  5. It can also be used as a message correlation id.
  6. Temporary file names.
  7. Transaction IDs.
  8. Unique identifiers for website users/visitors.
Generating UUID in Java based on host name & timestamp

You can use it as


300+ Java Interview FAQs

Tutorials on Java & Big Data