Open In App

How compare() method works in Java

Last Updated : 01 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Comparator Interface in Java, TreeSet in Java
The compare() method in Java compares two class specific objects (x, y) given as parameters. It returns the value: 

  • 0: if (x==y)
  • -1: if (x < y)
  • 1: if (x > y)

Syntax:  

public int compare(Object obj1, Object obj2)

where obj1 and obj2 are the two objects to be compared using compare() method. 

Example:

To show working of compare() method using Integer Class.

Java




// Java program to demonstrate working
// of compare() method using Integer Class
 
import java.lang.Integer;
 
class Gfg {
 
    // driver code
    public static void main(String args[])
    {
        int a = 10;
        int b = 20;
 
        // as 10 less than 20,
        // Output will be a value less than zero
        System.out.println(Integer.compare(a, b));
 
        int x = 30;
        int y = 30;
 
        // as 30 equals 30,
        // Output will be zero
        System.out.println(Integer.compare(x, y));
 
        int w = 15;
        int z = 8;
 
        // as 15 is greater than 8,
        // Output will be a value greater than zero
        System.out.println(Integer.compare(w, z));
    }
}


Output

-1
0
1

How is the return value evaluated:

The internal working of the compare() method can be visualized with the help of below pseudocode:

Java




// Converting the two objects to integer
// for comparison
int intObj1 = (int)obj1;
int intObj2 = (int)obj2;
 
// Get the difference
int difference = intObj1 - intObj2;
 
if (difference == 0) {
 
    // Both are equal
    return 0;
}
else if (difference < 0) {
 
    // obj1 < obj2
    return -1;
}
else {
 
    // obj1 > obj2
    return 1;
}


Visualizing the compare() method with this approach:

Java




// Java program to demonstrate working
// of compare() method
 
import java.lang.Integer;
 
class Gfg {
 
    // Function to compare both objects
    public static int compare(Object obj1, Object obj2)
    {
 
        // Converting the two objects to integer
        // for comparison
        int intObj1 = (int)obj1;
        int intObj2 = (int)obj2;
 
        // Get the difference
        int difference = intObj1 - intObj2;
 
        if (difference == 0) {
 
            // Both are equal
            return 0;
        }
        else if (difference < 0) {
 
            // obj1 < obj2
            return -1;
        }
        else {
 
            // obj1 > obj2
            return 1;
        }
    }
 
    // driver code
    public static void main(String args[])
    {
        int a = 10;
        int b = 20;
 
        // as 10 less than 20,
        // Output will be a value less than zero
        System.out.println(compare(a, b));
 
        int x = 30;
        int y = 30;
 
        // as 30 equals 30,
        // Output will be zero
        System.out.println(compare(x, y));
 
        int w = 15;
        int z = 8;
 
        // as 15 is greater than 8,
        // Output will be a value greater than zero
        System.out.println(compare(w, z));
    }
}


Output

-1
0
1

Various possible implementations of compare() method

public int compare(Object obj1, Object obj2)
{
Integer I1 = (Integer)obj1; // typecasting object type into integer type
Integer I2 = (Integer)obj2; // same as above ..
// 1.
return I1.compareTo(I2); // ascending order [0, 5, 10, 15, 20]
// 2.
return -I1.compareTo(I2); // descending order [20, 15, 10, 5, 0]
// 3.
return I2.compareTo(I1); // descending order [20, 15, 10, 5, 0]
// 4.
return -I2.compareTo(I1); // ascending order [0, 5, 10, 15, 20]
// 5.
return +1; // insertion order [10, 0, 15, 5, 20, 20]
// 6.
return -1; // reverse of insertion order [20, 20, 5, 15, 0, 10]
// 7.
return 0; // only first element [10]
}


Previous Article
Next Article

Similar Reads

Java Integer compare() method
The compare() method of Integer class of java.lang package compares two integer values (x, y) given as a parameter and returns the value zero if (x==y), if (x &lt; y) then it returns a value less than zero and if (x &gt; y) then it returns a value greater than zero. Syntax : public static int compare(int x, int y) Parameter : x : the first int to c
2 min read
Short compare() method in Java
The compare() method of Short class is used to compare two primitive short values for numerical equality. As it is a static method therefore it can be used without creating any object of Short. Syntax: public static int compare(short x, short y) Parameters: This method accepts two parameters: x: which is the first Short object to be compared. y: wh
2 min read
Boolean compare() method in Java with Examples
The compare() method of Boolean class is a built in method in Java which is used to compare two boolean values. It is a static method, so it can be called without creating any object of the Boolean class i.e. directly using the class name. Syntax: Boolean.compare(boolean a, boolean b) Parameters: It takes two boolean values a and b in the parameter
2 min read
Byte compare() method in Java with examples
The compare() method of Byte class is a built in method in Java which is used to compare two byte values. Syntax Byte.compare(byte a, byte b) Parameters: It takes two byte value 'a' and 'b' as input in the parameters which are to be compared. Return Value: It returns an int value. It returns: 0 if 'a is equal to 'b, a positive value 'a' is greater
2 min read
Float compare() Method in Java with Examples
The compare() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Syntax: public static int compare(float f1, float f2)Parameters: The function accepts two parameters: f1: The first floa
2 min read
Java Guava | Booleans.compare() method with Examples
The compare() method of Booleans Class in the Guava library is used to compare the two specified boolean values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public static int compare(boolean a, boolean b) Paramete
2 min read
Java Guava | Ints.compare() method with Examples
Ints.compare() is a method of Ints class in Guava library which is used to compare the two given int values. It takes two integers as parameters that are to be compared. It returns a comparator value based on the comparison of the specified integers.Syntax : public static int compare(int a, int b) Parameters: This method takes two parameters: a: wh
2 min read
Java Guava | compare() method of Short Class with Examples
Shorts.compare() method of Guava's Shorts Class is used to compare the two specified short values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public static int compare(short x, short y) Parameters: This method ac
2 min read
Java Guava | Floats.compare() method with Examples
Floats.compare() method of Floats Class is used to compare the two specified float values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public static int compare(float a, float b) Parameters: This method accepts tw
2 min read
Java Guava | Longs.compare() method with Examples
Longs.compare() method of Guava's Longs Class is used to compare the two specified long values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public static int compare(long a, long b) Parameters: This method accepts
2 min read
Practice Tags :