Java Integer compareUnsigned() method


The compareUnsigned() method compares two integer objects numerically considering the values as unsigned.

The return value if 0 if both values are equal; -1 if the val1is less than val2. The return value is 1, if the val1 is more than val2.

At first, set two Integer objects −

int val1 = 50;
int val2 = -10;

Now, compare them considering the values as unsigned −

System.out.println(Integer.compareUnsigned(val1, val2));

Following is an example to implement the compareUnsigned() method in Java −

Example

public class Main {
   public static void main(String[] args) {
      int val1 = 50;
      int val2 = -10;
      int val3 = 200;
      int val4 = 400;
      System.out.println(Integer.compareUnsigned(val1, val2));
      System.out.println(Integer.compareUnsigned(val2, val3));
      System.out.println(Integer.compareUnsigned(val1, val3));
      System.out.println(Integer.compareUnsigned(val3, val4));
      System.out.println(Integer.compareUnsigned(val3, val3));
   }
}

Output

-1
1
-1
-1
0

Updated on: 20-Sep-2019

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements