Hi Appu,

This has something to do with how equals() and hashcode() method are implemented in

java.

Let me first replicate the contract for these methods (which u've also mentioned):

Rule: If two objects are equal i.e. s1.equals(s2) is true then their hashcodes will always be equal.

If you look in this question, you can not determine what would be the respective hascodes of these

String and StringBuffer references. Rather you can tell if these refrences are equals() or not. Now using the rule above you can determine if the hashcodes are equal or not.

Strings have an overriden equals method in java. which checks the literal equality of values. And hence s1.equals(s2) is true in your code snippet. //1

StringBuffer use the Object class implentation, which is, if s1==s2 is true then equlas() will return true. sb1.equals(sb2) is false as sb1!=sb2. //2

Therefore hashcode is equal for s1 s2 but not sb1 sb2. Hence true false.