public class ObChk { public static void main(String[] args) { Ex e1=new Ex(1,2); Ex e2=new Ex(1,2); Ex e3=new Ex(4,5); Ex e4; e4=e2; if (e1.equals(e2)) { System.out.println("e1 and e2 Equal"); }
if (e1.equals(e3)) { System.out.println("e1 and e3 Equal"); } if (e2.equals(e4)) { System.out.println("e2 and e4 Equal"); } } }
The above program outputs only e2 and e4 are equal. can u tell me y ? thanks
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true). In your program you have not over-ridden the equals() method hence the equals() method of the Object class is invoked and since e4 and e2 refer to same Object it returns true.
[This message has been edited by sai challa (edited April 19, 2001).]