StringBuffer sb1 = new StringBuffer("Java");
StringBuffer sb2 = new StringBuffer("Java");
System.out.println(sb1 == sb2);
System.out.println(sb1.equals(sb2));
Here both are returning false. How is it possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
equalsmethod ofStringBufferis not overridden fromObject, so it is just reference equality, i.e., the same as using==. I suspect the reason for this is thatStringBufferis modifiable, and overridingequalsis mostly useful for value-like classes that you might want to use as keys (though lists also have an overriddenequalsandStringBufferis kind of a list, so this is a bit inconsistent).