Java Reference
In-Depth Information
17
18
StringBuilder lastBuffer =
new
StringBuilder(
"last buffer"
);
19
StringBuilder buffer =
new
StringBuilder();
20
21
buffer.append(objectRef)
.append(
"%n"
)
.append(string)
.append(
"%n"
)
.append(charArray)
.append(
"%n"
)
.append(charArray,
0
,
3
)
.append(
"%n"
)
.append(booleanValue)
.append(
"%n"
)
.append(characterValue);
.append(
"%n"
)
.append(integerValue)
.append(
"%n"
)
.append(longValue)
.append(
"%n"
)
.append(floatValue)
.append(
"%n"
)
.append(doubleValue)
.append(
"%n"
)
.append(lastBuffer);
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
System.out.printf(
"buffer contains%n%s%n"
, buffer.toString());
44
}
45
}
// end StringBuilderAppend
buffer contains
hello
goodbye
abcdef
abc
true
Z
7
10000000000
2.5
33.333
last buffer
Fig. 14.13
|
StringBuilder
append
methods. (Part 2 of 2.)
The compiler can use
StringBuilder
and the
append
methods to implement the
+
and
+=
String
concatenation operators. For example, assuming the declarations
String string1 =
"hello"
;
String string2 =
"BC"
;
int
value =
22
;
the statement
String s = string1 + string2 + value;