compareUnsigned()方法将两个整数对象视为无符号值进行数值比较。
如果两个值相等,则返回值为0;如果val1小于val2,则返回值为-1。 如果val1大于val2,则返回值为1。
首先,设置两个Integer对象-int val1 = 50;
int val2 = -10;
现在,比较它们,将值视为无符号-System.out.println(Integer.compareUnsigned(val1, val2));
以下是在Java中实现compareUnsigned()方法的示例-
示例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));
}
}
输出结果-1
1
-1
-1
0