What is string pool and what will happen when we creates a string? what does the pool contains?
Since am not aware with the basic concept, Please Please explain clearly.....
Thanks in advance.
Javardhan:
Ja vardhan
Ranch Hand
Joined: May 09, 2005
Posts: 169
posted
0
Frends,
I forgot to mention one more dowt:
whats the differance in using 'new' operator to create a string object? String str1 = "Ram"; String str2 = new String("Laxman"); /*remember, assigned values are differant for str1 and str2 */
String pool is the place where string literals live. It is somwhere in the heap itself.
String str1 = "Ram";
After this statement "Ram" string literal is created in the SLP (string literal pool) and str1 is referring to this literal.
String str2 = new String("Laxman");
After this statement "Laxman" string literal is is crated in the SLP, one more obejct is created on heap (because of new operator) and str2 is referring to this object (that is on heap) and this object is referring to "Laxman".
one more thing, string literal pool can't have two same string literal.
ganga prabhu
Greenhorn
Joined: Sep 29, 2005
Posts: 10
posted
0
one more thing, string literal pool can't have two same string literal.
No At runtime if a string is created through some string manipulation,a string is created in String pool and object is refer to it
No At runtime if a string is created through some string manipulation,a string is created in String pool and object is refer to it
That's not quite correct. A reference to the newly-created string will not be referenced in the runtime constant pool unless it is explicitly interned.
Example:
Akshay Kiran
Ranch Hand
Joined: Aug 18, 2005
Posts: 220
posted
0
why don't you do look into the Java Language Specification, you might find some more interesting things there, and more doubts too
"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
0
And just keep in mind that none of this is on the Exam. Good food for thought, but not on the exam.
Srinivas, Rathi, Ganag, Steve, Akshay, Corey... Thank you one and all for your sweet replies and sorry for the delayed reply.
Rathi posted..
---------------------------------------- String str2 = new String("Laxman");
After this statement "Laxman" string literal is is crated in the SLP, one more obejct is created on heap (because of new operator) and str2 is referring to this object (that is on heap) and this object is referring to "Laxman". ----------------------------------------- Still I am not clear with the differance. Pls explain.
Steve- Do you mean we can have two same string literals in the pool? Pls explain.
---------------------------------------- String str2 = new String("Laxman");
After this statement "Laxman" string literal is is crated in the SLP, one more obejct is created on heap (because of new operator) and str2 is referring to this object (that is on heap) and this object is referring to "Laxman". ----------------------------------------- Still I am not clear with the differance. Pls explain.
Close, but be careful with the terminology. There are two objects on the heap; one referred to by the literal "Laxman" and one referred to by the str2 reference. The objects will be equal (i.e., have the same contents).
Incidentally, you can get a reference to the same string object by calling the intern() method:
Steve- Do you mean we can have two same string literals in the pool?
No. Equal literals will always refer to the same String object. [ October 18, 2005: Message edited by: Steve Morrow ]