File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string pool" Watch "string pool" New topic
Author

string pool

Subhadeep biswas
Greenhorn

Joined: Apr 30, 2012
Posts: 14

String str1="i";
String Str2="i"+"am"+new String("string");
String str3="i"+"am"+"string";

can anyone explain how many string objects wiil be created in the pool as well as heap if any?

Thanks in advance
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Subhadeep biswas wrote:
can anyone explain how many string objects wiil be created in the pool as well as heap if any?

We would like to know your understanding first, so what you think and why?


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Subhadeep biswas
Greenhorn

Joined: Apr 30, 2012
Posts: 14

acc to me in the string pool strings would be "i","am","string","i am","i am string"..i don have idea on heaps existence...please help me in
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3645
    
  15
Subhadeep biswas wrote:"i am","i am string"

These two definitely won't exist. There are no spaces in your strings and concatenation doesn't automatically add them.

Subhadeep biswas wrote:acc to me in the string pool strings would be "i","am","string","i am","i am string"..i don have idea on heaps existence...please help me in

The compiler will optimise your code to

so the Strings in the pool will be i, iam, string and iamstring (due to the String literals)
There will also be a second String with the value string created by the new String call

All these Strings will be on the heap, although possibly in different parts of the heap.

Joanne
Subhadeep biswas
Greenhorn

Joined: Apr 30, 2012
Posts: 14

thanks for the help
if this is the case then the "string" (using new ) created in the heap where is the reference of it will be stored.

please explain me this scenario as well

String s1="i";
String s2=s1+"am String";

during runtime and compile time what happens..


Thanks in advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string pool