String Comparison using Equals

s.equals(x);

Will return true if the strings s and x contain the same character sequence. So:

String s = new String(“foobar”);
String x = new String(“foobar”);

Then s.equals(x) will return true, but s == x will be false, because s and x are not the same String Object.

If you then do:

String y = s;
String t = x;

You have a case where any string of the 4, .equals() any other string of the 4 (s, t, x, y) will return true, but only s == y or t == x will return true. That is because they refer to the same object.

Posted in Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *

*