As far as I know, StringBuilder helps to reduce memory usage by not creating temporary string instances in the string pool during concats. But, what happens if I do sth like this:
StringBuilder sb = new StringBuilder("bu");
sb.append("b"+"u");
Does it compile into
sb.append("b");
sb.append("u");
? Or it depends on optimalization flags? Or I loose the whole benefit if stringbuilders? Or this quetion makes no sense? :)
To concatenate 4 Strings you can just do s1 + s2 + s3 + s4 . If you need to concatenate using a loop, you should use a StringBuilder explicitly. In the example post linked, concat is faster because there is no overhead of creating a StringBuilder object.
append() method is used to append the string representation of some argument to the sequence.
In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java.
Note that regular string concatenations are faster than using the StringBuilder but only when you're using a few of them at a time. If you are using two or three string concatenations, use a string.
It compiles to sb.append("bu")
, because the compiler translates the concatenation of multiple String litterals to a single String litteral.
If you had
String a = "a";
sb.append(a + "b");
it would compile it to
String a = "a";
String temp = a + "b"; // useless creation of a string here
sb.append(temp);
So you should prefer
sb.append(a);
sb.append("b");
in this case.
Since "b" + "u"
is an expression which is evaluated at compile time, it will be compiled just as if you had "bu"
.
0: new #2; //class StringBuilder
3: dup
4: ldc #3; //String bu
6: invokespecial #4; //Method StringBuilder."<init>":(String;)V
9: astore_1
10: aload_1
11: ldc #3; //String bu
13: invokevirtual #5; // StringBuilder.append:(String;)LStringBuilder;
If you on the other hand had two string variables, this optimization wouldn't kick in:
The following snippet...
StringBuilder sb = new StringBuilder("bu");
String b = "b", u = "u";
sb.append(b + u);
...gets compiled as:
0: new #2; //class StringBuilder
3: dup
4: ldc #3; //String bu
6: invokespecial #4; //Method StringBuilder."<init>":(String;)V
9: astore_1
10: ldc #5; //String b
12: astore_2
13: ldc #6; //String u
15: astore_3
16: aload_1
17: new #2; //class StringBuilder
20: dup
21: invokespecial #7; //Method StringBuilder."<init>":()V
24: aload_2
25: invokevirtual #8; //Method StringBuilder.append:(String;)StringBuilder;
28: aload_3
29: invokevirtual #8; //Method StringBuilder.append:(String;)StringBuilder;
32: invokevirtual #9; //Method StringBuilder.toString:()String;
35: invokevirtual #8; //Method StringBuilder.append:(String;)StringBuilder;
I.e. something similar to
StringBuilder sb = new StringBuilder("bu");
String b = "b", u = "u";
StringBuilder temp = new StringBuilder();
temp.append(b);
temp.append(b);
String result = temp.toString();
sb.append(result);
As you can see in line 17-21 an extra StringBuilder
is created for the purpose of concatenating a
and b
. The resulting String
of this temporary StringBuilder
is then fetched on line 32 and appended to the original StringBuilder
on line 35.
(The bytecode was generated by the javap
command which is part of the JDK. Try it out, it's really simple!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With