Java Reference
In-Depth Information
4
public void
Test(String s) {
5 text = s;
6 }
7
8
public static void
main(String[] args) {
9 Test test =
new
Test(
"ABC"
);
10 System.out.println(test);
11 }
12 }
10.24
Show the output of the following code.
public class
Test {
public static void
main(String[] args) {
System.out.println(
"Hi, ABC, good"
.matches(
"ABC "
));
System.out.println(
"Hi, ABC, good"
.matches(
".*ABC.*"
));
System.out.println(
"A,B;C"
.replaceAll(
",;"
,
"#"
));
System.out.println(
"A,B;C"
.replaceAll(
"[,;]"
,
"#"
));
String[] tokens =
"A,B;C"
.split(
"[,;]"
);
for
(
int
i =
0
; i < tokens.length; i++)
System.out.print(tokens[i] +
" "
);
}
}
10.25
Show the output of the following code.
public class
Test {
public static void
main(String[] args) {
String s =
"Hi, Good Morning"
;
System.out.println(m(s));
}
public static int
m(String s) {
int
count =
0
;
for
(
int
i =
0
; i < s.length(); i++)
if
(Character.isUpperCase(s.charAt(i)))
count++;
return
count;
}
}
The
StringBuilder
and
StringBuffer
classes are similar to the
String
class
except that the
String
class is immutable.
Key
Point
In general, the
StringBuilder
and
StringBuffer
classes can be used wherever a string
is used.
StringBuilder
and
StringBuffer
are more flexible than
String
. You can add,
insert, or append new contents into
StringBuilder
and
StringBuffer
objects, whereas
the value of a
String
object is fixed once the string is created.
The
StringBuilder
class is similar to
StringBuffer
except that the methods for mod-
ifying the buffer in
StringBuffer
are
synchronized
, which means that only one task is
allowed to execute the methods. Use
StringBuffer
if the class might be accessed by multi-
ple tasks concurrently, because synchronization is needed in this case to prevent corruptions to
StringBuilder
Search WWH ::
Custom Search