Thursday, October 9, 2025
HomeLanguagesJavaUUID equals() Method in Java with Examples

UUID equals() Method in Java with Examples

The equals() method of UUID class in Java is used to check for the equality of one UUID with another. The method returns True if both the UUID’s are exactly identical bit by bit.

Syntax:

public boolean equals(Object uuidObj)

Parameters: The method takes one parameter uuidObj to which the UUID_1 is to be compared.

Return Value: The method returns true if both the UUIDs are identical else false.

Below programs illustrate the working of equals() method:

Program 1:




// Java code to illustrate equals() method
  
import java.util.*;
  
public class UUID_Demo {
    public static void main(String[] args)
    {
  
        // Creating two UUIDs
        UUID UUID_1
            = UUID
                  .fromString(
                      "5fc03087-d265-11e7-b8c6-83e29cd24f4c");
  
        UUID UUID_2
            = UUID
                  .fromString(
                      "5fc03087-d265-11e7-b8c6-83e29cd24f4c");
  
        // Displaying the UUID values
        System.out.println("UUID_1: "
                           + UUID_1.clockSequence());
        System.out.println("UUID_2: "
                           + UUID_2.clockSequence());
  
        // Comparing both the UUIDs
        System.out.println("Are both UUID equal: "
                           + UUID_1.equals(UUID_2));
    }
}


Output:

UUID_1: 14534
UUID_2: 14534
Are both UUID equal: true

Program 2:




// Java code to illustrate equals() method
  
import java.util.*;
  
public class UUID_Demo {
    public static void main(String[] args)
    {
  
        // Creating two UUIDs
        UUID UUID_1
            = UUID
                  .fromString(
                      "5fc03087-d265-11e7-b8c6-83e29cd24f4c");
  
        UUID UUID_2
            = UUID
                  .fromString(
                      "58e0a7d7-eebc-11d8-9669-0800200c9a66");
  
        // Displaying the UUID values
        System.out.println("UUID_1: "
                           + UUID_1.clockSequence());
        System.out.println("UUID_2: "
                           + UUID_2.clockSequence());
  
        // Comparing both the UUIDs
        System.out.println("Are both UUID equal: "
                           + UUID_1.equals(UUID_2));
    }
}


Output:

UUID_1: 14534
UUID_2: 5737
Are both UUID equal: false
RELATED ARTICLES

Most Popular

Dominic
32346 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6790 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS