String s1 = "testing";
String s2 = new String("testing");
System.out.println((s1.toString() == s2.toString()) ? "True":"False");
System.out.println(("another" == "another") ? "True":"False");
Output:
False
True
I know we should not use == to compare strings in Java because it will compare the references to these objects, not the strings itself and hence outputs would be unexpected. But if we compare both strings it should output true. Why is this happening? On the other hand, you can see that the second statement outputs true as excepted.