The code above compare two reference to the different String object. Pls remember String is immutable Class, So "String".replace('g','G') will result in creating reference to a new String object with value of "StringG". Two times excution will create two different reference, so the result of comparison is the false. Hope explanation makes sense.
As you said it will return false so i got the point, but why in the post by V Gala as shown below it is said it returns true........although both the statements are same?Is it i am making some mistake ?
i was going through the above discussion when i stumbled upon this piece of code... now i'm confused...
i ran this piece of code:
arit amit arit arit false true
Going by the above discussion, in this code , line 4 creates a new String Object with value 'arit'. Now when line 5 is executed shouldn't s3 point to the newly created 'arit' object? Wont it see in the String pool and find that such a String already exists and point the new references s3 and s4 to it?
But why am i getting false as the result of line 8? i'm confused ... please explain...
Thanks in advance Radha [ August 25, 2007: Message edited by: Radhika ]
Hi String s1 = new String("amit"); String s2 = "amit"; String s3 = new String("amit"); if(s1==s2) ans is false if(s1==s3) ans is false whwn we write s1.replace('m','r') it is eqivalent to String var= new String("arit");