String a=new String("amit"); String b="arit"; String c=a.replace('m','r'); System.out.println(b==c); when c String is created replace() returns "arit" which allready exists in the memory pool referenced by b. so why doesnt c refer to b? instead it creats a new String which results b==c to false Strings always check the memory pool if the new String already exists in memory, so y doesnt c pick b from pool rather it creates a new string why does this happen Thanks, pallavi
yes ofcourse here they refer to new objects as new is used...... the issue was about pooling ......i got my answer( i think!) String a=new String("amit"); String c=a.replace('r','m'); a==c now this is true as c is referring to the same pool a does String b="arit"; String c=b.replace('m','r'); b==c this is also true as c is referrinf to same pool b does String b="arit"; String c=a.replace('m','r');//c does not refer to b pool as we r using a.replace() System.out.println(b==c); // therefore its false i hope my logic is ryt
Use String.intern() to get the ref. fom the same string from the pool. Othrer things cannot assure the same string from the pool.. In fact new String() creates a new string itself irrespective of wheather it exists.. The declaration String s ="aaa"; String s1 = "aaa"; return u the same ref. as its a literal. try this out.
Pallavi, String.replace will actually create a new String with the "new" operator for you. It doesn't call intern() like Kaustubh mentioned. You would have to call replace().intern() before you could compare it to the other and get a true value (assuming the other was done the same way).
Thanks !! I mostly work with diagrams since they seem easy to remember i guess im a visual learner.The pictures make it interesting and it just sticks in mind. Hey Nathaniel How come with all SCXXY you stil looking for a job. I guess with such suffixes to your name company HR's gotta queuing down ya house.
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.