Generate a UUID in Kotlin

Kotlin is a statically typed, modern, and expressive programming language that runs on the Java Virtual Machine (JVM) and is fully interoperable with Java.

Developed by JetBrains and officially released in 2016, Kotlin aims to address various limitations and challenges posed by other programming languages while providing a more concise and safer syntax.

How to Generate a UUID in Kotlin

Kotlin's standard library doesn't include a dedicated UUID generation function. However, since Kotlin is fully interoperable with Java, we can use Java's java.util.UUID class to generate UUIDs in Kotlin code seamlessly.

import java.util.UUID;
fun main() {
// Generate a random UUID
val myUuid = UUID.randomUUID()
val myUuidAsString = myUuid.toString()
// Print the UUID
println("Generated UUID: $myUuid")
}

Explanation

  • On line #1, we import the UUID class. This class is part of the standard Java JDK. No 3rd party libraries are required.
  • On line #5, the UUID class' static method, randomUUID(), is used to generate a new, version 4 UUID. The generated UUID object is stored in the variable, myUuid.
  • Line #6 converts the myUuid object into a Java String by calling its toString() method. The String representation of a UUID looks like a standard UUID (i.e. d52a4216-0acc-43d7-ba00-d3ffdeecc59b). If you're going to store the UUID in a file, database or model property, or send it via an API call to a different application, you'll almost always need the String representation of the myUuid object, rather than the myUuid object itself.
  • Line #9 outputs the value of myUuidAsString and will look something like:
    Generated UUID: d52a4216-0acc-43d7-ba00-d3ffdeecc59b
    

Convert from a String to a UUID in Kotlin

Although it's rare, in some circumstances you might need to convert from a String representation of a UUID (like the one from line #6 above) back into an instance of Java's UUID.

In Kotlin, you again use Java's UUID class, which provides for this scenario with the static method, fromString(String). Here's a short example in Kotlin:

import java.util.UUID;
fun main() {
// Generate a random UUID
val myUuid = UUID.randomUUID()
val myUuidAsString = myUuid.toString()
// Assert the UUIDs are equal
val sameUuid = UUID.fromString(myUuidAsString)
assertTrue { sameUuid == myUuid }
}

Explanation

  • Line #9 shows converting the string representation of a UUID into a Java UUID instance (sameUuid) using the fromString(String) method.
  • Line #10 is included to show that the 2 UUID instances are equal.

How can we improve this page? Let us know!




The UUIDs generated by this site are provided AS IS without warranty of any kind, not even the warranty that the generated UUIDs are actually unique. You are responsible for using the UUIDs and assume any risk inherent to using them. You are not permitted to use the UUIDs generated by this site if you do not agree to these terms. Do not use any UUIDs found on cached versions of this page.
Privacy Policy
This website uses cookies. We use cookies to personalise content/ads and to analyse our traffic.

The UUIDs generated by this site conform to RFC 4122 whenever possible.
Read more about UUIDs at Wikipedia.
Check out our developer API.

Copyright © 2024 TransparenTech LLC. All Rights Reserved. Contact Us.