StringBuilder sb = new StirngBuilder (); sb.append ("a"). append ("b"). append ("c"). append ("d"); <>
String str = ""; str + = "a"; str + = "b"; str + = "c"; str + = "d"; <>
In the above example, "a", "b", "c", "d" should be first allocated space in the constant pool bar, so the above code should be similar at both ends, but why is the first efficient piece of code do? Seeking to explain
------ Solution -------------------------------------- ------
str + = "a"; decomposition point of view you want:
str = str + "a";
And str + "a" will create a new String object, it is slow. String objects you want to know that they can not be changed once created, to achieve the effect of string concatenation, you have to constantly create new objects.
StringBuilder until the last sb.toString () will create a String object, do not create new objects before (in your example, yes, but if you append a total length of more than a certain range - the default is a 16 - will create a new array to hold more String)
------ Solution ------------------------ --------------------
StringBuilder, and StringBuffer, strings are stored in a char [] in, char [] is stored in the heap. Compared
+ String is recreated every time a String object, re-opened a memory different, StringBuilder and StringBuffer append are directly in a String object char [] characters directly copied to a StringBuilder and StringBuffer char [ ], the efficiency is much higher than the String +. Of course, when StringBuilder and StringBuffer char [] is not long enough, it will also re-open a section of memory.
In addition, StringBuffer is thread-safe, StringBuilder is not thread safe.
------ Solution ------------------------------------ --------
javac will str + = "b"; into a StringBuilder the append operation.
equivalent of a new StringBuilder (). append ("a"). append ("b"). toString ().
calling the append than themselves much trouble.
StringBuilder builder = new StringBuilder ("a"); String s = builder.append ("b"). Append ("c"). Append ("d"). ToString ();
String a = "a"; a + = "b"; a + = "c"; a + = "d"; <>
javap-c output:
Code:
0: new # 2 ; / / class java / lang / StringBuilder
3: dup
4: ldc # 3 ; / / String a
6: invokespecial # 4 ; / / Method java / lang / StringBuilder. "
9: astore_1
10: aload_1
11: ldc # 5 ; / / String b
13: invokevirtual # 6 ; / / Method java / lang / StringBuilder.append: (Ljava / lang / String ;) Ljava / lang / StringBuilder; 16: ldc # 7 ; / / String c
18: invokevirtual # 6 ; / / Method java / lang / StringBuilder.append: (Ljava / lang / String ;) Ljava / lang / StringBuilder; 21: ldc # 8 ; / / String d
23: invokevirtual # 6 ; / / Method java / lang / StringBuilder.append: (Ljava / lang / String ;) Ljava / lang / StringBuilder; 26: invokevirtual # 9 ; / / Method java / lang / StringBuilder.toString: () Ljava / lang / String; 29: astore_2
30: ldc # 3 ; / / String a
32: astore_3
33: new # 2 ; / / class java / lang / StringBuilder
36: dup
37: invokespecial # 10 ; / / Method java / lang / StringBuilder. "
40: aload_3
41: invokevirtual # 6 ; / / Method java / lang / StringBuilder.append: (Ljava / lang / String ;) Ljava / lang / StringBuilder; 44: ldc # 5 ; / / String b
46: invokevirtual # 6 ; / / Method java / lang / StringBuilder.append: (Ljava / lang / String ;) Ljava / lang / StringBuilder; 49: invokevirtual # 9 ; / / Method java / lang / StringBuilder.toString: () Ljava / lang / String; 52: astore_3
53: new # 2 ; / / class java / lang / StringBuilder
56: dup
57: invokespecial # 10 ; / / Method java / lang / StringBuilder. "
60: aload_3
61: invokevirtual # 6 ; / / Method java / lang / StringBuilder.append: (Ljava / lang / String ;) Ljava / lang / StringBuilder; 64: ldc # 7 ; / / String c
66: invokevirtual # 6 ; / / Method java / lang / StringBuilder.append: (Ljava / lang / String ;) Ljava / lang / StringBuilder; 69: invokevirtual # 9 ; / / Method java / lang / StringBuilder.toString: () Ljava / lang / String; 72: astore_3
73: new # 2 ; / / class java / lang / StringBuilder
76: dup
77: invokespecial # 10 ; / / Method java / lang / StringBuilder. "
80: aload_3
81: invokevirtual # 6 ; / / Method java / lang / StringBuilder.append: (Ljava / lang / String ;) Ljava / lang / StringBuilder; 84: ldc # 8 ; / / String d
86: invokevirtual # 6 ; / / Method java / lang / StringBuilder.append: (Ljava / lang / String ;) Ljava / lang / StringBuilder; 89: invokevirtual # 9 ; / / Method java / lang / StringBuilder.toString: () Ljava / lang / String; 92: astore_3
93: return <>
------ For reference only ----------------- ----------------------
JAVA created String is not all put constant pool? For example sb.append ("a") in the "a" is not accounted for in the constant pool of space?
------ For reference only -------------------------------------- -
"a" is accounted for, "ab" not accounted for, the "ab" in the "a" + "b" when it is new, and sb.append ("a"). append ('b ") is no
------ For reference only ------------------------------ ---------
That reason is because StringBuilder fast that opens up space in advance, append it to the memory address is assigned; whereas the String always present to keep open space and therefore accounted for String space will be relatively large!?
------ For reference only -------------------------------- -------
than each String changes, but also to reproduce create a String object, and create new objects, there may be triggered GC
------ For reference only - --------------------------------------
personal understanding and a floor almost, " ab "in the" a "+" b "when it is the new
------ For reference only --------------------- ------------------
upstairs to answer very detailed, almost.
没有评论:
发表评论