• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pooling

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pallavi,
Just because two strings have the same value doesn't mean they will be the same object. Take this for example:
 
pallavi utukuri
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice reply
Yogesh Chhawasaria
 
Yogesh Chhawasaria
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks yogesh and thanks to everyone!
reply
    Bookmark Topic Watch Topic
  • New Topic