Java Reference
In-Depth Information
is equivalent to
System.out.printf(
String.format(format, item1, item2, ..., itemk));
where the square box (
) denotes a blank space.
✓
✓
9.1
Suppose that
s1
,
s2
,
s3
, and
s4
are four strings, given as follows:
Check
Point
String s1 =
"Welcome to Java"
;
String s2 = s1;
String s3 =
new
String(
"Welcome to Java"
);
String s4 =
"Welcome to Java"
;
What are the results of the following expressions?
a.
s1 == s2
b.
s2 == s3
c.
s1.equals(s2)
d.
s2.equals(s3)
e.
s1.compareTo(s2)
f.
s2.compareTo(s3)
g.
s1 == s4
h.
s1.charAt(
0
)
i.
s1.indexOf(
'j'
)
j.
s1.indexOf(
"to"
)
k.
s1.lastIndexOf(
'a'
)
l.
s1.lastIndexOf(
"o"
,
15
)
m.
s1.length()
n.
s1.substring(
5
)
o.
s1.substring(
5
,
11
)
p.
s1.startsWith(
"Wel"
)
q.
s1.endsWith(
"Java"
)
r.
s1.toLowerCase()
s.
s1.toUpperCase()
t.
"Welcome "
.trim()
u.
s1.replace(
'o'
,
'T'
)
v.
s1.replaceAll(
"o"
,
"T"
)
w.
s1.replaceFirst(
"o"
,
"T"
)
x.
s1.toCharArray()
9.2
To create the string
Welcome to Java
, you may use a statement like this:
String s =
"Welcome to Java"
;
or:
String s =
new
String(
"Welcome to Java"
);
Which one is better? Why?
9.3
Suppose that
s1
and
s2
are two strings. Which of the following statements or expres-
sions are incorrect?
String s =
new
String(
"new string"
);
String s3 = s1 + s2;
String s3 = s1 - s2;
s1 == s2;
s1 >= s2;
s1.compareTo(s2);
int
i = s1.length();
char
c = s1(
0
);
char
c = s1.charAt(s1.length());
9.4
What is the printout of the following code?
String s1 =
"Welcome to Java"
;
String s2 = s1.replace(
"o"
,
"abc"
);
System.out.println(s1);
System.out.println(s2);