• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is string pool?

 
Ja vardhan
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frends,

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.
 
Ja vardhan
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 */

-----------------------------------------------------------------

what will happen in this case:
String str1 = "ram";
String str2 = new Strin("ram");
/* here the values are same */

Thanks.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this article from ranch
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try,

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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 220
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why don't you do look into the Java Language Specification, you might find some more interesting things there, and more doubts too
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just keep in mind that none of this is on the Exam. Good food for thought, but not on the exam.
 
Ja vardhan
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Thanks a lot for your replies.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

----------------------------------------
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic