How is StringBuffer implementing append function without creating two objects?

后端 未结 10 1097
执念已碎
执念已碎 2021-01-30 09:22

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 operati

10条回答
  •  借酒劲吻你
    2021-01-30 09:52

    StringBuffer, like StringBuilder allocates an array of char into which it copies the strings you append. It only creates new objects when the number of characters exceeds the size of the array, in which case it reallocates and copies the array.

提交回复
热议问题