Search code examples
javaobjectgarbage-collectionstringbuffer

Is a new StringBuffer object created when the StringBuffer goes out of capacity or it remains still the old one?


Is a new StringBuffer object created when the StringBuffer goes out of capacity or it remains still the old one?

class Test{
public static void main(String[] args) {
    StringBuffer sb = new StringBuffer();
    StringBuffer sb2 = sb;
    System.out.println(sb.capacity());
    sb.append("abcdefghijklmnop");
    System.out.println(sb.capacity());
    sb.append("q");
    System.out.println(sb.capacity());
    System.out.println(sb == sb2);
 }
}

input: deep (master *) LanguagePackageInJava $ javac Lecture7.java
deep (master *) LanguagePackageInJava $ java Test
output: 16
16
34
true

In the above piece of code, I have tried to create a StringBuffer object sb and copied its reference to another StringBuffer object sb2 when the StringBuffer sb was empty. I have tried to overflow the capacity of sb and then tried out whether a new object has been created or not by comparing sb2 with sb. But it returns true. I suppose no object has been created. But I am not pretty sure about it even after going through the documentation. Any help would be appreciated!


Solution

  • The line

    StringBuffer sb2 = sb;
    

    doesn't create a copy of the sb object. It only creates another reference to the same object. Now you have two StringBuffer references to the same StringBuffer object.

    sb  -------> StringBuffer
              /
    sb2 ------
    

    If you overflow the capacity of a StringBuffer, it will reallocate an internal buffer for the contents.

    sb  -------> StringBuffer ----XXXXXXX"abcdefghijklmnop"
              /                  \
    sb2 ------                    ------>"abcdefghijklmnopq"
    

    The internal array used as a buffer for the contents will be a new object created. The old buffer will be garbage collected eventually.

    However, the StringBuffer object itself is not replaced. When you append something to a StringBuffer, it always returns a reference to itself. You're ignoring it, but even if you reassigned the return value back to sb, it's still referring to the same object.

    Also, you can replace StringBuffer with StringBuilder and the rest of the above would not change.