Skip to main content
Post Closed as "Not suitable for this site" by gnat, Robbie Dee, Christophe, Bart van Ingen Schenau, David Arno
added 129 characters in body
Source Link

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");

}

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 str2 will be GC'ed(Str 2 will not be interned) as thread comes out of method 2 and GC runs as it is stored in heap not permgen.

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 ?

Linked related string-literal-String-Object

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");

}

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 str2 will be GC'ed(Str 2 will not be interned) as thread comes out of method 2 and GC runs as it is stored in heap not permgen.

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 ?

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");

}

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 str2 will be GC'ed(Str 2 will not be interned) as thread comes out of method 2 and GC runs as it is stored in heap not permgen.

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 ?

Linked related string-literal-String-Object

deleted 103 characters in body
Source Link

When to use String Literal vs Creating String with equal operator vs new operator?

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 str1str2 will be GC'ed(Str 2 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 ?

When to use String Literal vs String with new operator?

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 2 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 ?

Creating String with equal operator vs new operator?

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");

}

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 str2 will be GC'ed(Str 2 will not be interned) as thread comes out of method 2 and GC runs as it is stored in heap not permgen.

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 ?

edited body
Source Link

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 32 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 ?

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 ?

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 2 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 ?

Source Link
Loading