public class ObjectEqualsComparison {
public static void main(String[] args) {
Object ob1 = new Object(); // address location 0×1234
Object ob2 = new Object(); // address location 0×2345
System.out.println(ob1 == ob2); // prints false correct
System.out.println(ob1.equals(ob2)); //prints false which is incorrect//it should be true.
//ob1 == ob2 gives false which is correct since you are comparing address/references .
//ob1.equals(ob2) is also giving false.can you tell me why since we are comparing contents
}
}
编辑::我的问题是我是否需要覆盖 equals() 方法。如果这样,equals() 覆盖的方法应该是什么样子。请帮忙