class strb
{
static public void main(String...string)
{
StringBuilder s1 = new StringBuilder(\"Test\");
StringBuilder s2 = new StringBuild
The default implementation of .equals
for the Object
class is as you mentioned.
Other classes can override this behavior. StringBuilder
is not one of them.
String is one of them, which overrides it to ensure that the String representations of both objects result in the same sequence of characters. String API
Refer to the documentation for the specific object in question.
Yes, StringBuilder does not override Object's .equals() function
, which means the two object references are not the same and the result is false.
For StringBuilder
, you could use s1.toString().equals(s2.toString())
For your edit, you're calling the ==
operator on two different String objects. The ==
operator will return false because the objects are different. To compare Strings, you need to use String.equals()
or String.equalsIgnoreCase()
It's the same problem you were having earlier