class strb
{
static public void main(String...string)
{
StringBuilder s1 = new StringBuilder("Test");
StringBuilder s2 = new StringBuilder("Test");
System.out.println(s1);
System.out.println(s2);
System.out.println(s1==s2);
System.out.println(s1.equals(s2)); //Line 1
System.out.println(s1.toString()==s2.toString()); //Line 2
if(s1.toString()==s2.toString())System.out.println("True"); //Line 3
}
}
And the output is
Test
Test
false
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 and Line 3 also 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