Java Utility Library

Java UUID - equals() Method



The java.util.UUID.equals() method is used to compare this object to the specified object. The result is true if and only if the argument is not null, is a UUID object, has the same variant, and contains the same value, bit for bit, as this UUID.

Syntax

public boolean equals(Object obj)

Parameters

obj Specify the object to be compared.

Return Value

Returns true if the objects are the same; false otherwise.

Exception

NA.

Example:

In the example below, the java.util.UUID.equals() method is used to compare given UUIDs.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating UUIDs
    UUID uid1 = UUID.fromString("d81e4f2e-cdf2-10e6-529b-7df92533d1cb");
    UUID uid2 = UUID.fromString("d81e4f2e-cdf2-10e6-529b-7df92533d1cb");
    UUID uid3 = UUID.fromString("c50e4f2e-cdf2-10e6-529b-7df92533d1cb");

    //comparing uuids
    System.out.println("Is uid1 equal to uid2: " + uid1.equals(uid2));  
    System.out.println("Is uid1 equal to uid3: " + uid1.equals(uid3));  
  }
}

The output of the above code will be:

Is uid1 equal to uid2: true
Is uid1 equal to uid3: false

❮ Java.util - UUID