To generate a random ID in Java, you can use the UUID
(Universally Unique Identifier) class from the java.util
package.
The UUID
class generates a random ID that is guaranteed to be unique across all devices, at all times.
Here's an example of how to generate a random ID in Java using the UUID
class:
UUID uuid = UUID.randomUUID(); String id = uuid.toString();
In the above example, a new UUID
object is generated using the randomUUID()
method, and the toString()
method is used to convert the UUID
to a String
representation. The resulting String
is stored in the id
variable.
For example, the id
variable might contain a value like "f81d4fae-7dec-11d0-a765-00a0c91e6bf6".
You can use this id
variable as a unique identifier for an object or record in your application.
For more information on the UUID
class and how to use it in Java, you can refer to the documentation in the Java API.