class returntest
{
public static void main(String...args)
{
String name1 = "Test";
String s = new String("Test");
StringBuilder sb = new StringBuilder("Test");
System.out.println(name1.equals(sb)); //Line 1
System.out.println(name1.equals(s)); //Line 2
System.out.println(s.equals(sb)); // Line 3
System.out.println(s.equals(name1)); //Line 4
}
}
The following is the output
false
true
false
true
Line 1 returns and Line 3 returns false.
I dont understand why compiler does not think "name1" and "sb" as containing the same value
Similarly compiler does not think "s" and "sb" as containing the same string (both are non-primitives).
Can someone explain the line1 and line3 output ?
See Question&Answers more detail:os