StringBuffer str1=new StringBuffer("hello1");
StringBuffer str2=new StringBuffer("hello1");
System.out.println(str1.equals(str2));
It will Showing False result Why?
StringBuffer str1=new StringBuffer("hello1");
StringBuffer str2=new StringBuffer("hello1");
System.out.println(str1.equals(str2));
It will Showing False result Why?
StringBuffer equals()
method isn't overridden to check content. It's using the default "shallow equals" that compares references it inherits from java.lang.Object.
So
StringBuffer str1=new StringBuffer("hello1");
StringBuffer str2=new StringBuffer("hello1");
System.out.println(str1.equals(str2));
is Comparing reference that is why you are getting false
There is no overriding of equals
in the StringBuffer
class. So it inherits the definition from Object
class. And from Java API we know its behavior:
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
You have two different objects, so equals
return false
in this case.