62,614
社区成员
发帖
与我相关
我的任务
分享
//Integer类下面的equals方法,如果这么用,会出现编译错误么?
new Integer(10).equals(10);
System.out.println(new Integer(10).equals(new Integer(10)));//最原始的用法,没问题;
System.out.println(new Integer(10).equals(10));//equals里面的10被编译器auto-boxing成Integer对象,也没有问题;
System.out.println(new Integer(130).equals(11111));//为什么这个没有编译问题呢?11111已经超过了auto-boxing的范围了
Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.
private final int value;
public Integer(int value) {
this.value = value;
}
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}