This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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?
Joined: Dec 01, 2004
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.
Joined: Dec 29, 2004
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.
Joined: Dec 01, 2004
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?
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