• 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 and the new operator question

 
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Waking up an old thread. I have a related question.

I am using operator new to construct a String object as shown below. Does this create a copy in both heap memory and String pool?



Staff note (Henry Wong) :

This question was split off from here ... https://coderanch.com/t/672484/java/String-immutable

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not taking escape analysis into consideration, that statement will create a new String object on the heap, but it won't affect the string pool (assuming "Raja" is already in the string pool).
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Also ... The String pool, along with "escape analysis", are implementation details. This means that they may change in the future -- between versions/platforms.

Henry
 
raja singh kumar
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Not taking escape analysis into consideration, that statement will create a new String object on the heap, but it won't affect the string pool (assuming "Raja" is already in the string pool).



What if "Raja" is not there in the string pool when that statement is getting executed? Will it make any change in the string pool?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raja singh kumar wrote:. . . What if "Raja" is not there in the string pool when that statement is getting executed? . . .

Things like the String pool and the structure of the heap may change from version to version. Escape analysis has already been mentioned; it was introduced in Java6. In Java8 the permanent generation disappeared. How do you know that there will still be a String pool in Java11? Maybe there will be something different.

"Raja" is shown in that code as a String literal. If that code is compiled and executed, the String literal "Raja" will be added to the String pool whenever the class is loaded. How can that String not be in the String pool?
 
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compile-time constant Strings are currently loaded lazily on first usage (LDC checks whether the constant has been resolved and resolves if necessary) -> If this is the only line using the literal "Raja", then it won't be in the pool until it's executed for the first time.
 
raja singh kumar
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My whole program is as follows. There is only one line. Will this code which is using new create a copy in both Heap memory and string constant pool? Also, does each class have its own string pool?



 
reply
    Bookmark Topic Watch Topic
  • New Topic