UUIDs
The Uuid class represents Universally Unique Identifiers (UUIDs), also known as Globally Unique Identifiers (GUIDs).
A Uuid is a 128-bit value used to uniquely identify an entity without relying on a central system that assigns IDs. This makes UUIDs useful in distributed applications, databases, client-generated records, or Kotlin Multiplatform applications.
Use the Uuid class to work with UUID values. Unlike plain strings, a dedicated UUID type makes your code more explicit and prevents accidental use of invalid values.
To use UUIDs in your project, import the Uuid class from the kotlin.uuid package:
Generate UUIDs
To generate a random version 4 UUID for regular identifiers, such as user or database IDs, use the Uuid.random() function:
You can also generate specific versions of UUIDs with the following Experimental functions:
The
Uuid.generateV4()function generates the same type of UUID as theUuid.random()function but explicitly states that the value is a version 4 UUID.The
Uuid.generateV7()function generates a version 7 UUID with a timestamp that you can use for UUID sorting.The
Uuid.generateV7NonMonotonicAt()function generates a version 7 UUID for a specific moment in time.
These UUID generation functions are Experimental. To opt in, use the @OptIn(ExperimentalUuidApi::class) annotation or add the following compiler option to your build file:
Here's an example that generates version-specific UUIDs:
Parse UUIDs
UUID values are often represented as strings, such as in URL parameters or database records.
To convert a String value to a Uuid value, use the Uuid.parse() function:
The Uuid.parse() function accepts both the standard hex-and-dash format and the hexadecimal format without dashes.
If the input is invalid, the Uuid.parse() function throws an IllegalArgumentException:
If your application accepts only one representation, use the format-specific functions:
Uuid.parseHexDash()for the hex-and-dash string representation.Uuid.parseHex()for the hexadecimal string representation without dashes.
For example:
If you have UUIDs from external sources and must handle invalid input safely, use Uuid.parseOrNull(), Uuid.parseHexDashOrNull(), or Uuid.parseHexOrNull(). These functions return null if the input is invalid:
Convert UUIDs to strings
You can convert a Uuid value to a String value with the following functions:
toString()for the standard string representationtoHexDashString()for the hex-and-dash formattoHexString()for the hexadecimal format without dashes
For example:
Compare UUIDs
You can check whether Uuid values are equal using the == operator.
Kotlin compares values according to the UUID value, not to the textual representation. For example, two values in different forms are equal if they represent the same 128-bit value:
This makes the Uuid comparison more reliable than comparing strings, which treats the same value in different formats as different values. Uuid comparison checks the actual identifier value.
Uuid implements the Comparable<Uuid> interface, so UUID values can be sorted with standard collection functions such as sorted(). In that case, Kotlin compares values lexicographically (from the most to the least significant bit):
Work with binary representations
Some APIs, storage formats, and binary protocols do not represent UUIDs as strings. Instead, they store the 128-bit UUID value as either:
A 16-byte array
Two 64-bit values
Use these representations when you need to exchange UUIDs with systems that expect binary UUID data.
To convert between a UUID and a 16-byte representation, use the .toByteArray() and Uuid.fromByteArray() functions:
You can also represent the same 128-bit UUID value as two Long values. This is useful because Kotlin doesn't provide a built-in 128-bit integer type. The two Long values store the UUID in two parts:
The
mostSignificantBitsparameter for the first 64 bits of a UUID.The
leastSignificantBitsparameter for the last 64 bits of a UUID.
To create a Uuid value from two Long values, use the Uuid.fromLongs() function:
To extract the two parts from an existing Uuid value, use the Uuid.toLongs() function:
Serialize UUIDs
Kotlin supports serialization for Uuid values. Use it to store or transfer a UUID value outside Kotlin code, for example, in JSON APIs or configuration files.
To serialize a Uuid value, represent it as a string unless your application requires another format. The kotlinx.serialization library uses the hex-and-dash format:
Use UUIDs with Java APIs
Java uses the java.util.UUID class to represent UUIDs. On the JVM, Java APIs may accept or return this type. Although java.util.UUID and kotlin.uuid.Uuid both represent UUIDs, they are two distinct types.
Convert values explicitly to pass UUIDs between Kotlin and Java:
Convert a Java UUID to Kotlin with the
.toKotlinUuid()extension function:import kotlin.uuid.toKotlinUuid val kotlinId: Uuid = javaId.toKotlinUuid()Convert a Kotlin UUID to Java with the
.toJavaUuid()extension function:import kotlin.uuid.toJavaUuid val javaId: java.util.UUID = kotlinId.toJavaUuid()
These functions allow you to represent your UUID values using Uuid at JVM interoperability boundaries.
Kotlin also provides support for working with Java buffers. Use JVM-specific functions to work with UUIDs in a ByteBuffer:
Use the
.getUuid()function to read a UUID from a buffer.Use the
.putUuid()function to write a UUID to a buffer.