aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is string pool? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is string pool? " Watch "what is string pool? " New topic
Author

what is string pool?

Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
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.


Javardhan:
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
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

Joined: Sep 28, 2004
Posts: 1228
check this article from ranch


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Sep 29, 2005
Posts: 10

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

Joined: May 22, 2003
Posts: 657

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
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
And just keep in mind that none of this is on the Exam. Good food for thought, but not on the exam.


SCJP Tipline, etc.
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
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

Joined: May 22, 2003
Posts: 657


----------------------------------------
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is string pool?
 
Similar Threads
About String pool
String Pool
Why not Equal?
String
String new object clarification