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've read that when you create String objects using "new" , 2 objects are created one of which a pool object which is simply given to a reference when its attempted to create a similar literal string, without creating a new object. Could anyone please tell me why I'm getting false below when the content of the String("Ravissant") is the same for both the references?
Using "new" creates a completely new reference. It is not using a String from the pool.
In your example, you have two objects created:
1. At Line 06, "Ravissant" is created in the pool
2. At Line 06, a new String object is created, whose value equals "Ravissant" (but different reference)
3. At Line 07, an object is referring to the "Ravissant" object in the pool.
So these are two different references.
First of all, thanks for your prompt reply. Ok, so when we do : String s1 = new String("Ravissant");
1) Two objects are created.
2) s1 points to the heap object 3) The second object is the pool object isnt being pointed to by any reference(?)
4) Later, when we do String s2= "Ravissant" , that "dangling/unassigned" object is given to the s1 reference.
Please let me know if the above deductions are correct.