0

On some blog and site even on stack overflow, I can see similar ans for below question

    String s = new String("Test");

Will create two object as literal "Test" will take place in pool.

So how StringBuffer perform in below case.

    StringBuffer sb = new StringBuffer("BufferTest");

Is the literal "BufferTest" also take place in pool ?

if yes then how StringBuffer save String garbage collection ?

1

2 Answers 2

0

"BufferTest" will take place in the pool because any String object does that.

Sign up to request clarification or add additional context in comments.

Comments

0

Is the literal "BufferTest" also take place in pool ?

Yes.

All literal strings are help in the string pool/

if yes then how StringBuffer save String garbage collection ?

It doesn't need to. A String corresponding to a string literal remains reachable so long as the class (or classes) that defined it is in existence / loaded.

2 Comments

But As per below code I think with every append call StringBuffer loosing reference of first. <br/> ' StringBuffer sb = new StringBuffer();' ' sb.append("Test");' ' System.out.println(sb.toString());' sb.append("Test1"); System.out.println(sb.toString());
Like I said, the StringBuffer doesn't need to keep a reference to it. The reference for "Test" is placed in the string pool when the code is loaded, not when it is executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.