This week's book giveaway is in the Testing forum. We're giving away four copies of Practical Unit Testing with TestNG and Mockito and have Tomek Kaczanowski on-line! See this thread for details.
Question 53. in Abhilash Java Quiz I would like to know whether StringBuffer can be used to compare string. I complied and run the foll codes and it shows nothing.
--------------------<BR>Java will never die.<P>Kenneth
Johannes de Jong
tumbleweed
Bartender
Joined: Jan 27, 2001
Posts: 5087
posted
0
Originally posted by Frank Carver: The two StringBuffers don't come out as equal, because StringBuffer just uses the "default" implementation of equals(), which does the same as '==' and directly compares the object references. The two StringBuffers are different objects with different references, so they come out as non-equal.
And
Originally posted by Hari Gangadharan: I really love this question! The StringBuffer *does* not override Object's "equals" method whereas String do. Hence when you invoke s1.equals(s2), the equals method of Object is invoked. The equals method of Object just does a shallow compare (which essentially is s1 == s2) and returns false! Hope this helps --
[This message has been edited by Johannes de Jong (edited April 06, 2001).]
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18652
posted
0
The JavaRanch forum in intended for discussion of this web site, not questions about Java itself. That's what all the other forums here are for. I'm moving this discussion to Java in General (Beginner).