From K&B 5.. page 494..
regarding StringBuffer class.. its mentioned in the book that StringBuffer equals is not overridden, it doesnt compare values.. so i tried a program and i observed that equals method works on StringBuffer.. and output is "az"..
how is that??? is it that the "Hello" value of StringBuffer is stored in the String literal pool?? i know the second variable is pointing to the same object ..
If the equals method is not overriden for any class then the equals method checks whether the reference variables refer to the same object
if they refer to the same object then the equals method returns true
also == operator checks whether the bit pattern is same in those variables and if two variables refer to the same object
then the bit pattern is same for them and hence == return true for them
so in your question
buffer and buffer2 refer to the same "Hello"
equals method and == operators return true and az gets printed
I had tried a sample program that can help you understand a little more
this is it
you are forgetting something
I have mentioned before that
If a class does not override the equals method then the equals method return true if those objects refer to the same object
here in this case
StringBuffer does not override the equals method
we have written that
sb1 = sb4;
sb1.equals(sb4) returns true
hope this is clear to you now
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right