Update:
Answering to your doubt in the comment:
Here is a diagram I got from internet:

This explains clearly why str3 !=str4
and str3 !=str5
(because the moment you do a concatenation, a new object is created.
And when you do something like String s2 = str2;
s2 basically is the same reference to the String object str2. This is true for any object in Java. Luckily strings are immutable, but if str2
was some other mutable object, then if you had made any changes in s2
, str2
would also have changed.

As @Paul mentioned in the comment, ==
tests whether they are the same object reference in memory. But .equals(..)
tests for value equality i.e to find out if they are logically equal.
First, str==s because you are doing
String s = str;
i.e s is the reference to the same object as str. So both are equal.
Same with s2.
Now,
String str3 = str + str2;
String str4 = "helloworld";
here str3 and str4 are different object references. So they are not equal.
Same with str3 and str5