• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string pool

 
Greenhorn
Posts: 14
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic