---
schema_version: "1"
id: 8611592a5fd38957df3ce4941afbcbff94202423c6effb4ef1e09c9d8fed595c
title: UUID Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_class_System_UUID.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_UUID.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 78127ace5b8f2b93e64113aa79d02099a60b13fbce8f0b9d7b93799db90ee277
description: Contains methods to randomly generate a version 4 universally unique identifier (UUID), compare UUIDs, and convert UUID instance to a string.Compares a UUID instance with the specified object and returns true if both are equal. Otherwise, returns false.Converts a 32 character hexadecimal string representation of a UUID to a UUID instance.Returns the hashcode corresponding to the UUID instance. A static method that randomly generates a version 4 UUID.Returns the string representation of the UUID instance.
---
# UUID Class

Contains methods to randomly generate a version 4 universally unique identifier (UUID), compare UUIDs, and convert UUID instance to a string.

## Namespace

[System](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_System.htm "The System namespace provides classes and methods for core Apex functionality.")

## Usage

The UUID is generated using a cryptographically strong pseudo-random number generator and is represented as 32 hexadecimal values.

- **[UUID Methods](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_UUID.htm#apex_System_UUID_methods)**

## UUID Methods

The following are methods for UUID.

- **[equals(obj)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_UUID.htm#apex_System_UUID_equals)**  
    Compares a UUID instance with the specified object and returns true if both are equal. Otherwise, returns false.
- **[fromString(str)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_UUID.htm#apex_System_UUID_fromString)**  
    Converts a 32 character hexadecimal string representation of a UUID to a UUID instance.
- **[hashCode()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_UUID.htm#apex_System_UUID_hashCode)**  
    Returns the hashcode corresponding to the UUID instance.
- **[randomUUID()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_UUID.htm#apex_System_UUID_randomUUID)**  
    A static method that randomly generates a version 4 UUID.
- **[toString()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_UUID.htm#apex_System_UUID_toString)**  
    Returns the string representation of the UUID instance.

### 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")

#### Example

// UUIDs are equal when all the characters in the UUID are the same
String uuidStr = '707b2538-98bb-41e7-95e3-1d77bf42b102';
UUID fromStr = UUID.fromString(uuidStr);
UUID fromStr2 = UUID.fromString(uuidStr);
Assert.isTrue(fromStr.equals(fromStr2));

// A UUID is never equal to a String or any non-UUID object
Assert.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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: System.UUID

#### Example

String uuidStr = '707b2538-98bb-41e7-95e3-1d77bf42b102';
UUID fromStr = UUID.fromString(uuidStr);

UUID.fromString(null); // Throws NullPointerException

UUID.fromString('not a uuid'); // Throws IllegalArgumentException

### hashCode()

Returns the hashcode corresponding to the UUID instance.

#### Signature

public Integer hashCode()

#### Return Value

Type: [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_methods_system_integer "Contains methods for the Integer primitive data type.")

### 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

UUID randomUUID = UUID.randomUUID();
System.debug(randomUUID); // Prints the UUID string that was randomly generated

### toString()

Returns the string representation of the UUID instance.

#### Signature

public String toString()

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")