Integer.compare() and Integer.compareUnsigned() methods

compare() method is used to compare two values numerically, whereas compareUnsigned() compares two values by treating them as unsigned. This post will look in detail at the Integer class’s compare() and compareUnsigned() methods.

Let’s look at both of them one by one.

public static int compare(int x, int y)

compare() is a static method introduced in Java 1.7, which will return 1, 0, and -1 after numerically comparing the two values. Below are the scenarios for the same.

If x = y , it will return 0
if x > y , it will return 1
if x < y, then it will -1

Code Example
public class Codekru {

	public static void main(String[] args) {

		int x = 28;
		int y = 8;

		System.out.println("compare function returned: " + Integer.compare(x, y));

		int m = 1000;
		int n = 1000;

		System.out.println("compare function returned: " + Integer.compare(m, n));

		int i = 8;
		int j = 28;

		System.out.println("compare function returned: " + Integer.compare(i, j));

	}
}

Output –

compare function returned: 1
compare function returned: 0
compare function returned: -1
Internal implementation of compare() function

The compare() function internally uses the ternary operator to compare two values passed in the arguments.

    public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }
Time Complexity of the Integer compare() function

The time complexity of the compare() function is O(1) as it is only comparing two primitive values.

public static int compareUnsigned(int x, int y)

  • What does it do? compareUnsigned() is a static method introduced in Java 1.8 that compares the two numbers as they are unsigned.
  • What does it return? After comparing the arguments as unsigned, it will also return -1, 0, or 1.

Now, what is signed and unsigned integers? Signed integers contain positive and negative numbers, ranging from [-2147483648 to 2147483647], whereas unsigned integers contain only non-negative numbers ranging from [0 to 4294967295].

So, if we pass any negative number to the compareUnsigned() function, it will be treated as a positive number after adding Integer.MIN_VALUE ( -2147483648 ) to it. For example, number -8 will be treated as 2147483640.

System.out.println(-8+Integer.MIN_VALUE);  // this will print "2147483640"
Comparing two positive numbers using compareUnsigned
public class Codekru {

	public static void main(String[] args) {

		int x = 28;
		int y = 8;

		System.out.println("compareUnsigned function returned: " + Integer.compareUnsigned(x, y));

		int m = 1000;
		int n = 1000;

		System.out.println("compareUnsigned function returned: " + Integer.compareUnsigned(m, n));

		int i = 8;
		int j = 28;

		System.out.println("compareUnsigned function returned: " + Integer.compareUnsigned(i, j));

	}
}

Output –

compareUnsigned function returned: 1
compareUnsigned function returned: 0
compareUnsigned function returned: -1
Comparing one positive number and one negative number using compareUnsigned
public class Codekru {

	public static void main(String[] args) {

		int x = 28;
		int y = -8;

		System.out.println("compareUnsigned function returned: " + Integer.compareUnsigned(x, y));

		System.out.println("compare function returned: " + Integer.compare(x, y));

	}
}

Output –

compareUnsigned function returned: -1
compare function returned: 1

What happened here?

  • -8 was converted to an unsigned number by adding Integer.MIN_VALUE.
  • The new number that came up was 2147483640.
  • So, compareUnsigned then compared between 28 and 2147483640
  • And that’s why it returned -1.
Internal implementation of compareUnsigned(int x, int y) function

The compareUnsigned() function internally uses the compare() function.

    public static int compareUnsigned(int x, int y) {
        return compare(x + MIN_VALUE, y + MIN_VALUE);
    }
Time Complexity of the Integer compareUnsigned(int x, int y) function

compareUnsigned() function internally calls the compare() function which has O(1) time complexity. So, the time complexity of the compareUnsigned() function will also be O(1).

Please visit this link to learn more about the Integer wrapper class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

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