Reputation: 15
userText will be a string of around 7000 characters in different languages. I was wondering how the strings will be garbage collected after executing this code. For suppose after unescapeHtml4 userText is assigned a new value and the same thing with after replace.
what happens to the previous string of userText. will they be in string pool or will be removed by the garbage collector.
String userText = context.getRequestParameter( "addedText");
if ( someCondition)
{
userText = StringEscapeUtils.unescapeHtml4( userText ) );
}
else
{
userText = userText.replace( charsequence1, charsequence2 );
}
-- some logic using userText ---
Upvotes: 1
Views: 429
Reputation: 85
//This String object will live as long as "context" will live
String userText = context.getRequestParameter( "addedText"); //"addedText" goes to a String Pool
if ( someCondition)
{
//This String object will live as long as "userText" variable is accessible
userText = StringEscapeUtils.unescapeHtml4( userText ) );
}
else
{
//This String object will live as long as "userText" variable is accessible
userText = userText.replace( charsequence1, charsequence2 );
}
Verb "live" means existence of the object before GC will have a right to kill it;-)
Upvotes: 1