| Author |
pooling
|
pallavi utukuri
Ranch Hand
Joined: Feb 10, 2004
Posts: 182
|
|
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
|
Thanks,<br />Pallavi
|
 |
Nathaniel Stoddard
Ranch Hand
Joined: May 29, 2003
Posts: 1258
|
|
Pallavi, Just because two strings have the same value doesn't mean they will be the same object. Take this for example:
|
Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
|
 |
pallavi utukuri
Ranch Hand
Joined: Feb 10, 2004
Posts: 182
|
|
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
|
 |
Kaustubh Patil
Ranch Hand
Joined: Aug 13, 2001
Posts: 164
|
|
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.
|
Kaustubh. Mumbai, India.
|
 |
Nathaniel Stoddard
Ranch Hand
Joined: May 29, 2003
Posts: 1258
|
|
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).
|
 |
Yogesh Chhawasaria
Ranch Hand
Joined: Apr 02, 2004
Posts: 53
|
|
Consider this When you say Two objects are created. And foll creates one object String b = "arit"; Now when u execute String c = a.replace('m','r'); one new object is created in Object Space So final view is b and c point to diff objs hence false.
|
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
|
 |
Nathaniel Stoddard
Ranch Hand
Joined: May 29, 2003
Posts: 1258
|
|
Yogesh! That was about the most beautiful reply I've ever seen!! How did you do those cool little diagrams? [ April 17, 2004: Message edited by: Nathaniel Stoddard ]
|
 |
atiqur rahman
Greenhorn
Joined: Aug 30, 2003
Posts: 10
|
|
Nice reply Yogesh Chhawasaria
|
 |
Yogesh Chhawasaria
Ranch Hand
Joined: Apr 02, 2004
Posts: 53
|
|
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.
|
 |
pallavi utukuri
Ranch Hand
Joined: Feb 10, 2004
Posts: 182
|
|
|
Thanks yogesh and thanks to everyone!
|
 |
 |
|
|
subject: pooling
|
|
|