Reputation: 3321
class strb
{
static public void main(String...string)
{
StringBuilder s1 = new StringBuilder("Test");
StringBuilder s2 = new StringBuilder("Test");
System.out.println(s1); // output: Test
System.out.println(s2); // Test
System.out.println(s1==s2); // false
System.out.println(s1.equals(s2)); //Line 1 output: false
System.out.println(s1.toString()==s2.toString()); //Line 2 output: false
}
}
Just have a quick question on .equals.
Regardless of the object content, does .equals
return true only if both the object references point to the same object ?
EDIT : Now I understand the part about the .equals
but why does Line 2 not return true
?
EDIT : I believe ==
looks at the reference variable's address and so s1 and s2 cannot be equal.correct me if my assumption is not right
Upvotes: 36
Views: 64564
Reputation: 2588
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.
Upvotes: 69
Reputation: 154
As others have said, StringBuilder
does not override equals
. However, it does implement Comparable
(since Java 11). Therefore, you can check s1.compareTo(s2) == 0
to find out if the string representations are equal.
Upvotes: 1
Reputation: 387
Since StringBuilder does not have a equals method, it is best to first change the StringBuilder to String and then check equality.
Eg - sb1.toString().equals(sb2.toString())
.
Note: The == operator or sb1 == sb2
will never work here because the two StringBuilder(s) are completely different objects.
Upvotes: 0
Reputation: 14199
for your first answer check @abmitchell 's Answer
And for your Edit:
In Java, String is an object and we can't compare objects for value equality by using ==
==
is used for comparing primitives values or object references.
To compare Object values we use equals()
in Java
Upvotes: 2
Reputation: 7695
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.
Upvotes: 3
Reputation: 959
StringBuilder
and StringBuffer
not override the equals function of Object
class.but string override the equals
method.
the function of Object
is this
public boolean equals(Object obj) {
return (this == obj);
}
you could write your code like this.
System.out.println(s1.toString() == s2.toString());
System.out.println(s1.toString().equals(s2.toString()));
Upvotes: 1
Reputation: 4712
StringBuilder
class doesn't have the implementation of equals()
method like one in the String
class.
So it executes default Object class functionality, which again checks only for address equivalency, which is not same in this case. so it prints false.
Note 1: You can use ==
operator on objects also, but it simply checks if the address of both the objects are same or not.
Note 2: ==
operator plays good role in comparing two String objects created in string constant pool, to find out if it is really creating a new object in string constant pool or not.
Upvotes: 1
Reputation: 2167
Agreed with both of the above responses, but I'm worth noting to actually compare contents you can do:
System.out.println(s1.toString().equals(s2.toString())); //Line 1
Upvotes: 0
Reputation: 2189
Check the contract of equals
method:
it must be consistent (if the objects are not modified, then it must keep returning the same value).
That's why StringBuilder does not override it regardless of its content.
Let's take example above.
StringBuilder s1 = new StringBuilder("Test");
StringBuilder s2 = new StringBuilder("Test");
Maybe, to you it is expected that s1.equals(s2)
returns true
due to current run time values.
But what about if you change add line:
s1.append("abc");
Then s1 and s2 will have different string contents and s1.equals(s2)
is expected to be false
. But it is in contradiction with consistency.
Upvotes: 2
Reputation: 280141
The StringBuilder
class does not provide an overriden equals()
method. As such, when that method is called on an instance of StringBuilder
, the Object
class implementation of the method is executed, since StringBuilder extends Object
.
The source code for that is
public boolean equals(Object obj) {
return (this == obj);
}
Which simply compares reference equality.
Upvotes: 4