The line String s2=s1.replace('m','r'), this actually creates a new object in the heap.It means that s2 now is pointing on a different object.When you are going to use the == for comparison it will compare both operands via their reference type. It is another story if you are going to use the equal operator(s2.equals(s3) which will turn out true.Both operands are compared by their value.
Joined: Dec 16, 2003
Another thing, String are immutable. So if you you use any of its methods it actually creates another String object.