==
compares content of variables. (You know this very well from intA == intB
.)
A String
variable contains a reference to a String
object, so ==
will compare references.
After
String str1 = "Hi there";
String str2 = new String("Hi there");
str1
and str2
will refer to different string objects, thus contain different references, so str1 == str2
will yield false
.
str1.equals(str2)
on the other hand will compare the objects that str1
and str2
refers to, which, as you have noted, yields true
.