Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Strings and String pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Strings and String pool" Watch "Question on Strings and String pool" New topic
Author

Question on Strings and String pool

Monica Moncho
Ranch Hand

Joined: Dec 01, 2004
Posts: 31
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.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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


Mike Gershman
SCJP 1.4, SCWCD in process
shandilya popuru
Ranch Hand

Joined: Dec 21, 2004
Posts: 95
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


sandy
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
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

Joined: Dec 01, 2004
Posts: 31
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

Joined: Dec 29, 2004
Posts: 1071
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

Joined: Dec 01, 2004
Posts: 31
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

Joined: Mar 13, 2004
Posts: 1272
Yes
Udegbunam Morah
Ranch Hand

Joined: Oct 13, 2002
Posts: 36
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.


Udegbunam Ikechukwu Morah<br />B.Sc.,SCJP, MCP, OCP, PMP<br />Manager, Business COnsulting Group<br />Deep Business Solutions Limited
Monica Moncho
Ranch Hand

Joined: Dec 01, 2004
Posts: 31
thanks to all of you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Strings and String pool