Thursday, 8 August 2013

String Scope : regular heap vs pool

String Scope : regular heap vs pool

I know how String pool works and all. That said:
void doSomething1(){
System.out.println("This string is now added to the string pool and will
stay there even after this method returns");
}
void doSomething2(){
String msg = new String("This string is now added to the string pool and
will stay there even after this method returns");
System.out.println(msg);
}
Is doSomething2 better than doSomething1. Should this be encouraged, given
that some Strings have a very low chance of being reused. The problem I
see is in case 1, the string stays in scope for extended period even when
it is not used again.

No comments:

Post a Comment