Answer: No output The default behaviour of class Object equals() method is to compare the reference values. If the reference values are the same, then true is returned, otherwise false. To see an example of this, play around with the String class and StringBuffer class. The wrapper String class overrides the equals() method to compare the string literals, but the StringBuffer class just compares the reference values! String s1 = new String("abc"); String s2 = new String("abc"); s1.equals(s2); // returns true StringBuffer sb1 = new StringBuffer("abc"); StringBuffer sb2 = new StringBuffer("abc"); sb1.equals(sb2); // return false [ January 23, 2002: Message edited by: Rajinder Yadav ]
<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Seany Iris
Ranch Hand
Joined: Jan 08, 2002
Posts: 54
posted
0
But the answer is "Prints true". Maybe it is wrong.isn't it?
Oh Marcus, Its great to see a mail from a person like u. Well in this question i think that even there is no need to cast the object of ThisClass, b'coz every object is a blueprint of Object class bydefault. And this can be more understandable if explain it by an example- Suppose we have a vehicle class, which gives us a methodology that whenever we can implement, we get a new vehicle. And suppose we have a car class, which extends the vehicle class, and provide another object or methodology or formula or blueprint that on implementing gives us a car. but this does not mean that methodology of implementing the vehicle is also the method for implementing a car. HTH -Nisheeth
I just cut and paste the code from above compiled and ran ( just to be sure ) and I deffinatly get no output from it. I think if in doubt with some code the best bet is to follow Marcus's sugestion and compile and run the code to see what it does, from there you can go on to work out why it's done it. Mark
Seany Iris
Ranch Hand
Joined: Jan 08, 2002
Posts: 54
posted
0
I got it! I compiled and run it, and it print out nothing! The answer is wrong!
The answer is No output. Why ? Because the method equals() be overridden. The equals() method in Object and all Object extends it. So you can override this method in new SubClass. String , Integer .... etc class all override this method, so you can see if their content is same the result is true. But more object do not rewrite this method, the result is all false, like StringBuffer or Untitled5 . Exp: public class Untitled5 { public static void main( String[] args[]){ Object obj0 = new Object(); Object obj1 = new Object(); Object obj2=(Object) new Untitled5() ; Untitled5 u5_0 = new Untitled5() ; Untitled5 u5_1 = new Untitled5() ; Integer i_0 = new Integer(3); Integer i_1 = new Integer(3); System.out.println("obj0 equals obj1 ? "+obj0.equals(obj1)); System.out.println("obj0 equals obj2 ? "+obj0.equals(obj2)); System.out.println("u5_0 equals u5_1 ? "+u5_0.equals(u5_1)); System.out.println("i_0 equals i_1 ? "+i_0.equals(i_1)); } } The result is : obj0 equals obj1 ? false obj0 equals obj2 ? false u5_0 equals u5_1 ? false i_0 equals i_1 ? true