Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Question on Strings and String pool

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

I think I understood this from the book.
Java has a constant String pool where all strings are added, when the compiler encounters a string it checks to see if it already exists in the pool, and if it does instead of creating it it points the reference to the one in the pool. But if we create the String with keyword new the pool is not checked.

So, if I have string abc in the pool but then do String s = new String("abc"); do I now have 2 abc in the pool, just one but the abc is created as a new object?

Thanks for your help.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if I have string abc in the pool but then do String s = new String("abc"); do I now have 2 abc in the pool, just one but the abc is created as a new object?


You have one literal string "abc" in the constant pool and one String object with the value "abc" on the heap.
s == "abc" is false
s.equals("abc") is true
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
creating a string with new actually creates 2 objects
one on the heap and the other in the pool(if one does not exist prevoiusly)
and the reference variable points to the object on the heap


string pool is an abstract term, there is no physical thing
string pool is actually a collection of references to string objects on the heap


if u assign a literal like "java" to the string it is added to the string pool
and a refrence to it is passed to the variable
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shandilya popuru:
creating a string with new actually creates 2 objects
one on the heap and the other in the pool(if one does not exist prevoiusly)
and the reference variable points to the object on the heap



I don't think creating a String with new actually creates two Strings. I don't think one get's created in the pool with new. Even if it was put in the pool that would be a reference and not the object so why create an extra String just to put an unreachable reference in the pool?
 
Monica Moncho
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You have one literal string "abc" in the constant pool and one String object with the value "abc" on the heap.
s == "abc" is false
s.equals("abc") is true



so if I had not used new but s="abc" then I would have been pointed to the one in the pool. And if I did t="abc" then it would point to the pool too and s == t would be true?

Am I understanding?

Sorry for the confussion.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.


Although I would never rely on the == comparison in actual code. Not because it is unreliable, but it is more fragile in case of change.
 
Monica Moncho
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have done a small test and I have found out this with a little code:

String s1 = "111";
String ss1 = "111";

s1 == ss1 is true, so the first 111 went to the pool and the second one got the reference to the existing one.

Then I did in another program:

String s1 = new String ("111");
String ss1 = "111";

In the second case, s1 == ss1 is false, so the new didn't put anything in the pool.

This is what I understand from the test. Do you agree?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's look at this example:


The result prints the following on the screen:

The result of the comparison s1 == s2 is true
The result of the comparison s2 == s3 is false
The result of the comparison s1.equals(s3) is true

Note:
1. The == operator checks the references stored in each variable. In this case, s1 and s2 variables contain the same reference to a String literal created in a String pool.

2. Similar to (1.) above, s1 and s3 does not contain the same reference, since the new operator creates a String object containing the literal "abc", whether the literal exists in the String pool or not.

3. The equals() method compares the actual String literals (enclosed within the memory area whose references are stored in variables s1 and s3)not the value of the variables' (s1 and s3) references.

I hope this will give some insight.
 
Monica Moncho
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to all of you.
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic