有人可以用简单的语言解释我,为什么这个代码会抛出异常,"比较方法违反了它的一般合同!",我该如何修复它?
private int compareParents(Foo s1, Foo s2) {
if (s1.getParent() == s2) return -1;
if (s2.getParent() == s1) return 1;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我明天有一个测试,我无法理解我的书籍解释,我很感激帮助:
public class TestClass{
public static void main(String[] args) throws Exception{
int a = Integer.MIN_VALUE;
int b = -a;
System.out.println( a+ " "+b);
}
}
Run Code Online (Sandbox Code Playgroud)
输出: -2147483648 -2147483648
为什么这会打印2个相同幅度的负数而不是正数和负数?