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".