0

StringBuffer str = new StringBuffer("Java"); Where would String "Java" be created? Normal heap or constant pool?

Next if I modify it as- str.append(" nine");

Where would the modification happen ? Would it modify the string "Java" in constant pool and convert it to "Java nine"?

2
  • Please don't use StringBuffer as it was replaced by StringBuilder in 2004. Commented Jun 22, 2018 at 19:52
  • 1
    Where it is created depends on the version of Java you are using. The use of StringBuffer dates the question to being before Java 5.0, when it was stored in the perm gen (which no longer exists in the last 3 versions of Java) Commented Jun 22, 2018 at 19:53

2 Answers 2

7

StringBuffer str = new StringBuffer("Java"); Where would String "Java" be created? Normal heap or constant pool?

That statement doesn't create a String object. It creates a StringBuffer object and in the process copies the characters in the string "Java" into the character array maintained internally by str. The string "Java" already exists before that statement runs. Roughly speaking, it was created in the constant pool when the class containing this code was initialized.

Next if I modify it as- str.append(" nine");

Where would the modification happen ? Would it modify the string "Java" in constant pool and convert it to "Java nine"?

The modification happens in the internal character array maintained by str. The original string "Java" is unaffected. String objects in Java are immutable.

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

2 Comments

Thanks a ton Mr.Ted ! Your precise explanation has clarified my doubt.
It might be worth mentioning that running the first command will lead to allocation of the StringBuffer's internal array and it will copy the characters "Java" into it, so now the characters will be stored twice - once in the constant pool in the String and second time on heap inside StringBuffer's internal character array.
0

When you execute

new StringBuffer("Java")

, the string "Java" is initially stored in the heap memory as a part of the StringBuffer object. The StringBuffer class does not use the string constant pool for storing its content. So, "Java" here is stored in the normal heap memory.

on doing

str.append(" nine");

Here, str is a StringBuffer object. When you call append("nine"), the StringBuffer modifies its internal character array to include " nine".

String "Java" in Constant Pool: The string "Java" that was originally in the constant pool (due to its literal "Java" in the new StringBuffer("Java") statement) remains unchanged in the constant pool. It is not modified.

Internal Modification: The modification happens within the StringBuffer object's internal character array in the heap memory. The StringBuffer itself manages its own character data and does not affect the original string literal "Java".

Comments

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.