Java Integer compareUnsigned() method
The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned.
Syntax
public static int compareUnsigned(int x , int y)
Parameters
The parameters ‘x’ and ‘y’ represent the first and second int value to compare.
Return Value
This method returns an integer value.
- It returns zero, if both the parameters are equal(x==y).
- It returns value greater than zero, if x is greater than y as unsigned values(x>y).
- It returns value less than zero, if x if smaller than y as unsigned values (x<y).
Example 1
public class JavaIntegerCompareUnsignedExample1 {
public static void main(String[] args) {
byte b1=-9;
byte b2=-10;
int val=Integer.compareUnsigned(b1, b2);
System.out.println("compareUnsigned method will return "+val);
}
}
Output
compareUnsigned method will return 1
Example 2
public class JavaIntegerCompareUnsignedExample3 {
public static void main(String[] args) {
byte b1=-9;
byte b2=-10;
int val=Integer.compareUnsigned(b1, b2);
if (val==0){
System.out.println(b1+" and "+b2+" are equal");
}
else if (val>0){
System.out.println(b1+" is greater than "+b2);
}
else{
System.out.println(b2+" is greater than "+b1);
}
}
}
Output
-9 is greater than -10
