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
This doesn't compile.
String S= "orange";
S.append("apple");
if you do
final String S= "orange";
final S2 = S + "apple";
This doesn't create any objects as it is optimised at compile time to two String literals.
StringBuilder s = new StringBuilder("Orange");
s.append("apple");
This creates two objects StringBuilder
and the char[]
it wraps. If you use
String s2 = s.toString();
This creates two more objects.
If you do
String S= "orange";
S2 = S + "apple";
This is the same as
String S2 = new StringBuilder("orange").append("apple").toString();
which creates 2 + 2 = 4 objects.
tl;dr: In simple words, each string concatenation expression using the +
character leads to a new String
object with the contents of the initial strings being copied to the new one. StringBuffer
holds an internal structure that expands only when needed, that characters are appended to it.
Hey, but lots of people use the +
string concatenation!
Well, we/they shouldn't.
In terms of memory usage, you are using an array in StringBuffer
in order to hold the characters - that resizes, truth, but rarely if the algorithm applied in resizing is efficient, and only one String
object that is created once toString()
is called, much better than the creation of a new String
object on each +
concatenation.
In terms of time complexity, characters are copied only once from _chars
to the new string (O(n)
time complexity), which in general is must better than string concatenation using the +
operator, on which each operation leads to a new copy of the characters to a new object, leading to O(1 + 2 + .... + n) = O(n^2)
operations.
Should I implement one on my own?
It would be good for you in terms of excercise, but modern languages provide native StringBuffer
implementations to use it in production code.
In four simple steps:
MyCustomStringBuilder
class that internally (privately) holds an array (let's name it _chars
) of characters of a fixed initial size. This array will hold
the string characters._chars
once
the holding string character length exceeds its length. (What you
are practically doing, is implementing a simple version of an
ArrayList
internally).stringBufferInstance.append(String s)
method, add
characters to _chars
, increasing its size if needed.In your toString()
method implementation, you can simply create a string using the array:
public String toString() {
return new String(_chars);
}
As others described, StringBuffer
is mutable and it is implemented by using a char
array. Operations in the StringBuffer
are in-place operations.
More INFO can be available from the following link http://www.concentric.net/~ttwang/tech/jfastbuf.htm
It shows simple StringBuffer implementations using a char
array.
The source is your friend, Luke!
Here is the source for AbstractStringBuilder
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.
****String s1="Azad"; ----One object will create in String cons. pool
System.out.println(s1);--output--Azad
s1=s1.concat("Raja"); Two object will create 1-Raja,2-AzadRaja and address of AzadRaja Store in reference s1 and cancel ref.of Azad object
System.out.println(s1); --output AzadRaja****