• 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

Strings and String pools ...confusing at times...

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know about the concept of string pool but looks like not clearly ...after seeing these examples...
1)"String".replace('t','t')=="String".replace('t','t')
2)"String".replace('t','t')=="String"
3)"String".replace('g','G')=="String".replace('g','G')
4)"String".replace('g','G')=="StrinG"

The answers are true,true,false and false...
Intriguing!!!
Why are 2 and 4 giving different answers?
Is it because the string "String" is already present in the pool before the methods were invoked ?
I need help desperatley.. !!!
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These tests are to see if the String object references are the same.
In (1) and (2) they are pointing to the same String object. Since the call to replace doesn't modify the String object, the original String object reference is returned.
In (3) and (4) the String is modified, so a new String object is created and returned.
 
Sravanthi Dasari
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That answers my question but I have some more doubts...
If I have a string "place" in the string pool and I say
"rlace".replace('r','p') doesnt the returned string point to the one already in the pool? Does it create a new reference like
String something=new String("place") ?
I have read in Rbert et el
===============
Java classes have a pool of strings.When a literal is compiled ,the compiler adds an appropriate string to the pool.However,if the same literal already appeared as a a literal elsewhere in class, then it is already represented in the pool.The compiler doesn't create a new copy,it uses the existing one from the pool.This saves on memory and can do no harm.
=====================
So .when it uses the same copy from the pool when it encounters the same string again elsewhere in the program,doesnt it use the same reference also ?
If I had code like this...
String s=new String("StrinG");
if("String",replace('g','G')=="StrinG")
{
}
What is the scenario like now?
What are the strings pointing to the literal pool?
 
reply
    Bookmark Topic Watch Topic
  • New Topic