forked from tugul/CoreJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringVsStringBuilder.java
More file actions
45 lines (38 loc) · 1.71 KB
/
Copy pathStringVsStringBuilder.java
File metadata and controls
45 lines (38 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package strings;
/**
* String and StringBuilder's difference are highlighted
* - Concatenation create new object in String, but not in StringBuilder
* - String methods' return value must be used, otherwise new value is lost
*/
public class StringVsStringBuilder {
public static void main(String[] args) {
/////////////////////////////////////////////
// Difference 1:
/////////////////////////////////////////////
// Immutable class, create 27 new objects, garbage collector works busy to clean them
String str = "";
for (char c='a'; c <= 'z'; c++)
str += c;
// Mutable class, create only 1 object
StringBuilder sb = new StringBuilder();
for (char c='a'; c <= 'z'; c++)
sb.append(c);
/////////////////////////////////////////////
// Difference 2:
/////////////////////////////////////////////
// Immutable class method(i.e. concat) returns reference of newly concatenated
String a = "ab";
a = a.concat("cd"); // abcd
// while Mutable class method(i.e. append) returns reference of the same object changed
StringBuilder b = new StringBuilder("ab");
b.append("cd"); // abcd
b.append(true); // abcdtrue
b.append(5); // abcdtrue5
b.append((String)null); // requires explicit cast
b.append((StringBuilder)null); // requires explicit cast
// RESULT: abcdtrue5nullnull
// Similarity is method chaining which can be done on both
String st = " abc ".trim().toUpperCase().concat("D").substring(2); // CD
StringBuilder sbr = new StringBuilder().append("ABC").reverse(); // CBA
}
}