Java UUID.equals(Object obj) method example

In this tutorial, You will learn how to check if the two UUID's are equal or not. Java UUID.equals(Object obj) method returns true if the objects are the same.

Compares 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 - The object to be compared.

Returns

true if the objects are the same; false otherwise.

Java UUID equals(Object obj) example

  • UUID.fromString("6224c18d-d004-11bd-984e-b8d80b2bdff8"); It creates a UUID from the string standard representation.
  • uuid1.equals(uuid2); It will compare uuid1 to uuid2. In this case it returns true, because both UUID are same.
  • uuid1.equals(uuid3); It will compare uuid1 to uuid3. In this case it returns false, because both UUID are different.
package com.technicalkeeda;

import java.util.UUID;

public class App {

    public static void main(String[] args) {

        UUID uuid1 = UUID.fromString("6224c18d-d004-11bd-984e-b8d80b2bdff8");
        UUID uuid2 = UUID.fromString("6224c18d-d004-11bd-984e-b8d80b2bdff8");
        UUID uuid3 = UUID.fromString("5289df73-7df5-3326-bcdd-22597afb1fac");

        System.out.println("Is uuid1 equal to uuid2 ? " + uuid1.equals(uuid2));
        System.out.println("Is uuid1 equal to uuid3 ? " + uuid1.equals(uuid3));
    }
}

output

Is uuid1 equal to uuid2 ? true
Is uuid1 equal to uuid3 ? false