• Post Reply Bookmark Topic Watch Topic
  • New Topic

String pool and the new operator question  RSS feed

 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • 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

 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • 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).
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 189
1
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • 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?
 
Tobias Bachert
Ranch Hand
Posts: 85
18
  • Mark post as helpful
  • send pies
  • 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: 189
1
  • Mark post as helpful
  • send pies
  • 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?



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!