I have seen most of times developer declares the string in below fashion
Approach 1:-
public void method1(){ String str1 ="Test"; }
Approach 2:-
Per my understanding better approach will be
public void method2(){ String str2 = new String("Test"); str2.concat("new"); }
Again based on my understanding , Second approach is better than first, because String literal are interned and stored in permgen. So it will not be GC’ed even thread comes out of approach 1 but in second approach str1 will be GC’ed(Str 3 will not be interned) as thread comes out of method 2 and GC runs as it is stored in heap not permgen.
Third is better than second so that new object is not created while append.
Is my understanding correct ?
Per mine understanding I should literal if same string is going to be created again and again as it will be good for performance point of view otherwise go for new String() so that can be GC’ed once not used ?