Integer.compare() in Java

Integer.compare() in Java | In this blog, let us see about the compare method in the Integer wrapper class. Along with Integer.compare() method we will also see the compareTo() and compareUnsigned() methods.

The compare() method compares two primitive int values and returns 0 if both the int value are equal, else if the first int value is lesser than the second int value then returns the value lesser than 0, else if the first int value is greater than the second int value then returns the value greater than 0. It is a static method. There are two more similar methods that do the same task we will be explaining all three methods here.

Integer.compare() Method Syntax

The syntax for the Integer.compare() method and similar methods are as follows:-

  1. public static int compare(int x, int y)
  2. public int compareTo(Integer anotherInteger)
  3. public int compareUnsigned(int x, int y)

Parameters,
x and y are the int values that are to be compared.
anotherInteger: this is the second integer value that is compared with the given integer value in the compareTo method.
Return type: int

Return value,
0: if both the values are equal
> 0: if the first number is greater
< 0: if the first number is lesser.

How To Compare Two Integers In Java

The below program demonstrates the working of the compareTo() method which compares two Integer objects numerically.

public class Main {
    public static void main(String args[]) {
        Integer value1 = Integer.valueOf(40);
        Integer value2 = Integer.valueOf(500);
        System.out.println(value1.compareTo(value2));

        Integer value3 = Integer.valueOf(200);
        Integer value4 = Integer.valueOf(200);
        System.out.println(value3.compareTo(value4));

        Integer value5 = Integer.valueOf(250);
        Integer value6 = Integer.valueOf(10);
        System.out.println(value5.compareTo(value6));
    }
}

Output:

-1
0
1

How To Compare Two Primitive Int Values In Java

The below code is demonstrating compare(int x, int y) method which compares two int values numerically. The value returned is identical to what would be returned by:- Integer.valueOf(x).compareTo(Integer.valueOf(y))

public class Main {
    public static void main(String args[]) {
        int value1 = 45;
        int value2 = 550;
        System.out.println(Integer.compare(value1, value2));

        int value3 = 200;
        int value4 = 200;
        System.out.println(Integer.compare(value3, value4));

        int value5 = 250;
        int value6 = 10;
        System.out.println(Integer.compare(value5, value6));
    }
}

Output:

-1
0
1

How To Compare Two Int Values In Java Ignoring Sign

If we want to compare two primitive int values without taking care of their sign then we can use compareUnsigned(int x, int y) method.

public class Main {
    public static void main(String args[]) {
        int value1 = 699;
        int value2 = 122;
        System.out.println(Integer.compareUnsigned(value1, value2));

        int value3 = 566;
        int value4 = 566;
        System.out.println(Integer.compareUnsigned(value3, value4));

        int value5 = 44;
        int value6 = 100;
        System.out.println(Integer.compareUnsigned(value5, value6));
    }
}

Output:-

1
0
-1

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *