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?
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?
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 singh kumar wrote:. . . What if "Raja" is not there in the string pool when that statement is getting executed? . . .
"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?