Home »
Java programming language
Java UUID compareTo() Method with Example
UUID Class compareTo() method: Here, we are going to learn about the compareTo() method of UUID Class with its syntax and example.
Submitted by Preeti Jain, on February 21, 2020
UUID Class compareTo() method
- compareTo() method is available in java.util package.
- compareTo() method is used to compare two UUID objects or in other words, it is used to compare this UUID with the given object (ob).
- compareTo() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- compareTo() method does not throw an exception at the time of comparing two objects.
Syntax:
public int compareTo(UUID ob);
Parameter(s):
- UUID ob – represents the UUID object to be compared with this UUID.
Return value:
The return type of the method is int, it returns any one of the given three values [0,1 and -1],
- When (This UUID) < (UUID ob), it returns -1.
- When (This UUID) == (UUID ob), it returns 0.
- When (This UUID) > (UUID ob), it returns 1.
Example:
// Java program to demonstrate the example
// of int compareTo(UUID ob) method of UUID
import java.util.*;
public class CompareToOfUUID {
public static void main(String[] args) {
// Instantiate UUID
UUID uuid1 = UUID.fromString("46400000-8cc0-11bd-b43e-10d46e4ef14d");
UUID uuid2 = UUID.fromString("56500000-8cd0-10bd-b65e-10d46e4ef14d");
// By using compareTo() method is
// to compare this object with the given object
int result = uuid1.compareTo(uuid2);
System.out.println("uuid1.compareTo(uuid2): " + result);
}
}
Output
uuid1.compareTo(uuid2): -1