class Value
{
int i;
}
public class EqualsMethod
public static void main(String args[])
{
Value v1=new Value();
Value v2=new Value();
v1.i=v2.i=100;
System.out.println(v1.equalss(v2));
}
}
为啥是false? ,书上说这是由于equals的默认行为是比较引用。所以除非在自己的新类中覆盖equals()方法。
调试欢乐多
对象的成员变量与对象的引用没有任何关系.
but have different memory reference, since v1 is a object and v2 is another object
return (this == obj);
}
if (obj == this) return true;
if (obj == null) return false;
if (obj.getClass() != this.getClass()) return false;
Value other = (Value) obj;
return this.i == other.i;
}
易载--个人网上资料收集必备工具。选中想收藏的文字(图片),右键选“收录到易载”,搞定!
这么写会报错,这样就没事了System.out.println((v1.i)==(v2.i))