• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string pool

 
Subhadeep biswas
Greenhorn
Posts: 14
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Subhadeep biswas
Greenhorn
Posts: 14
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Subhadeep biswas
Greenhorn
Posts: 14
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic