when you create S1 string is created and reference is stored in s1 when you create s2 with same string JVM doesn't create string at another location but stores the reference of earlier string in s2 Thus s1 == s2 returns true;
but when you manupulate s1 and store in s3 string is created at new location and with the desire change. thus s3 has different reference
same with s2 s4 is created with new ref. and thus s3 == s4 false as s3 and s4 are not same
Hope its correct please anyone can justify this
Dream!!! Set Vision !! Transform Vision to Mission by Actions on Deadlines!.
I think the thing to remember is that a String object is just a pointer (in C speak). That is the JVM maintains a pool of Strings and you can have many many String Object just pointing at the same instance of a string in the JVM, but each of the String objects is a different object instance, therfore different memory location.
When you do a == you are comparing the memory location of the String Object not the underlying character representation of the String.
What is happening is that compiler and JVM are assigning s1 and s2 the same String object. You can see this by doing the following
the moment you do
you are creating new String objects (look at the API for the replace method). Therefore the == will compare the memory location of the different String objects and return false, whereas s3.equals(s4) should return true.