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?
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
posted 1 year ago
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?