Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6765883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:45:54+00:00 2026-05-26T14:45:54+00:00

It was an interview question. I was asked to implement the StringBuffer append function.

  • 0

It was an interview question. I was asked to implement the StringBuffer append function. I saw the code after the interview. But I cannot understand how the operation is done with creation of a single object.

I am thinking like this.

String s = "orange";
s.append("apple");

Here two objects are created.

But

StringBuilder s = new StringBuilder("Orange");
s.append("apple");

Now here only one object is created.

How is Java doing this operation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:45:55+00:00Added an answer on May 26, 2026 at 2:45 pm

    First there is a problem with your question:

    String s = "orange";
    s.append("apple");
    

    here two objects are created

    Correct, two Objects are created, the String “orange” and the String “apple”, inside the StringBuffer/StringBuilder no Objects will be created if we don’t overflow the buffer. So those lines of code create 2 or 3 objects.

    StringBuilder s = new StringBuilder("Orange");
    s.append("apple");
    

    Now here only one object is created

    I don’t know where you get that, here you create one StringBuilder Object, one “Orange” String, one “apple” String, for a total of 3 Objects, or 4 if we overflow the StringBuilder buffer. (I count the array creation as object creation).


    I read your question as, how can StringBuilder do the append without creating a new Object (when the buffer is not overflown)?

    You should look at StringBuilder, since it’s the non thread safe implementation. The code is interesting and easy to read. I’ve added the inline comments.

    As internal structure there is a char array, not a String. It is initially built with length 16 and will be increased every time the capacity is exceeded. If the Strings to append fit within the char array, there is no need to create new Objects.

    StringBuilder extends AbstractStringBuilder, where you’ll find the following code:

    /**
     * The value is used for character storage.
     */
    char value[];
    

    Since not all the array will be used at a given time, another important variable is the length:

    /**  
     * The count is the number of characters used.
     */
    int count;
    

    There are many overloading of append, but the most interesting one is the following:

    public AbstractStringBuilder append(String str) {
        if (str == null) str = "null"; //will literally append "null" in case of null
        int len = str.length(); //get the string length
        if (len == 0) return this; //if it's zero, I'm done
        int newCount = count + len; //tentative new length
        if (newCount > value.length) //would the new length fit?
            expandCapacity(newCount); //oops, no, resize my array
        str.getChars(0, len, value, count); //now it will fit, copy the chars 
        count = newCount; //update the count
        return this; //return a reference to myself to allow chaining
    }
    

    String.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Copies characters from this string into the destination character array.

    So, the append method is quite simple, the only magic left to discover is the expandCapacity, here it is:

    void expandCapacity(int minimumCapacity) {
        //get the current length add one and double it
        int newCapacity = (value.length + 1) * 2; 
        if (newCapacity < 0) { //if we had an integer overflow
            newCapacity = Integer.MAX_VALUE; //just use the max positive integer
        } else if (minimumCapacity > newCapacity) { //is it enough?
            //if doubling wasn't enough, use the actual length computed
            newCapacity = minimumCapacity;
        }
        //copy the old value in the new array
        value = Arrays.copyOf(value, newCapacity); 
    }
    

    Arrays.copyOf(char[] original, int newLength) Copies the specified array, truncating or padding with null characters (if necessary) so the copy has the specified length.

    In our case, padding, since we’re expanding the length.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was asked an interview question where I needed to use it but I
I was asked this question in an interview and I gave various solutions but
This is an interview question asked a month ago.... Do session use cookies? If
This was the question asked in interview. Can we call one constructor from another
Here one more basic question asked in MS interview recently class A { public
Recently I have been asked an interview question What are the events order in
I had telephone interview question yesterday. The interviewer asked me if I had faced
This question was asked at interview. Say I have a contract. [ServiceContract] public interface
This question was asked to me in an interview. Suppose char *p=malloc(n) assigns more
I was asked this question during an interview. They're both O(nlogn) and yet most

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.