Reputation: 123
I am aware about interning, but what if I had something like the following:
String str = "before";
str = "after";
Since they are not the same string, would a new object of String be created for "after", and if so what happens to "before"?
Just to clarify, when does interning automatically happen? Only when two Strings with the same literal are created?
Upvotes: 0
Views: 100
Reputation: 36304
"before" will be added to the String pool when it is created. then when "after" is created then it is also added to the String pool. Only the reference will point to "after" now instead of before.
NOTE : they will be "created" only if they are not already present in the String Pool.
Upvotes: 1