forked from cirosantilli/java-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBufferCheat.java
More file actions
24 lines (19 loc) · 803 Bytes
/
StringBufferCheat.java
File metadata and controls
24 lines (19 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
# StringBuilder
http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
Mutable string. Implements `CharSequence` which is the main interface of String.
May be more efficient than `+` on String as it may avoid string object creation.
Smart compilers can optimize this out for simple `+`: http://stackoverflow.com/a/8595943/895245
but not necessarily inside loops: http://stackoverflow.com/a/30451908/895245
So just use it yourself.
*/
public class StringBufferCheat {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("ab");
sb.append("cd");
sb.append("ef");
assert sb.toString().equals("abcdef");
// ERROR: + does not work: the magic is just for String
//sb = sb + sb;
}
}