UUID Class
Contains methods to randomly generate a version 4 universally unique identifier (UUID),
compare UUIDs, and convert UUID instance to a string.
Namespace
Usage
The UUID is generated using a cryptographically strong pseudo-random number generator and is represented as 32 hexadecimal values.
UUID Methods
The following are methods for UUID.
equals(obj)
Compares a UUID instance with the specified object and returns true if both are equal.
Otherwise, returns false.
Signature
public Boolean equals(Object obj)
Parameters
- obj: Type: Object The UUID object to be compared.
Return Value
Type: Boolean
Example
1// UUIDs are equal when all the characters in the UUID are the same
2String uuidStr = '707b2538-98bb-41e7-95e3-1d77bf42b102';
3UUID fromStr = UUID.fromString(uuidStr);
4UUID fromStr2 = UUID.fromString(uuidStr);
5Assert.isTrue(fromStr.equals(fromStr2));
6
7// A UUID is never equal to a String or any non-UUID object
8Assert.isFalse(fromStr.equals(uuidStr));fromString(str)
Converts a 32 character hexadecimal string representation of a UUID to a UUID
instance.
Signature
public static System.UUID fromString(String str)
Parameters
- str: Type: String
Return Value
Type: System.UUID
Example
1String uuidStr = '707b2538-98bb-41e7-95e3-1d77bf42b102';
2UUID fromStr = UUID.fromString(uuidStr);
3
4UUID.fromString(null); // Throws NullPointerException
5
6UUID.fromString('not a uuid'); // Throws IllegalArgumentExceptionhashCode()
Returns the hashcode corresponding to the UUID instance.
Signature
public Integer hashCode()
Return Value
Type: Integer
randomUUID()
A static method that randomly generates a version 4 UUID.
Signature
public static System.UUID randomUUID()
Return Value
Type: System.UUID
A 32 hexadecimal value of the UUID generated.
Example
1UUID randomUUID = UUID.randomUUID();
2System.debug(randomUUID); // Prints the UUID string that was randomly generatedtoString()
Returns the string representation of the UUID instance.
Signature
public String toString()
Return Value
Type: String