Java Reference
In-Depth Information
The expression is passed as a string in one argument and it is split into three parts—
tokens[0]
,
tokens[1]
, and
tokens[2]
—using the
split
method (line 15) with a space
as a delimiter.
Integer.parseInt(tokens[0])
(line 19) converts a digital string into an integer. The
string must consist of digits. If it doesn't, the program will terminate abnormally.
For this program to work, the expression must be entered in the form of “operand1 opera-
tor operand2”. The operands and operator are separated by exactly one space. You can mod-
ify the program to accept the expressions in different forms (see Programming Exercise 9.28).
9.22
✓
✓
This topic declares the
main
method as
Check
Point
public static void
main(String[] args)
Can it be replaced by one of the following lines?
public static void
main(String args[])
public static void
main(String[] x)
public static void
main(String x[])
static void
main(String x[])
9.23
Show the output of the following program when invoked using
1.
java Test I have a dream
2.
java Test “1 2 3”
3.
java Test
public class
Test {
public static void
main(String[] args) {
System.out.println(
"Number of strings is "
+ args.length);
for
(
int
i =
0
; i < args.length; i++)
System.out.println(args[i]);
}
}
K
EY
T
ERMS
interned string
337
wrapper class
350
C
HAPTER
S
UMMARY
1.
Strings are objects encapsulated in the
String
class. A string can be constructed
using one of the 13 constructors or simply using a string literal. Java treats a string lit-
eral as a
String
object.
2.
A
String
object is immutable; its contents cannot be changed. To improve effi-
ciency and save memory, the JVM stores two literal strings that have the same char-
acter sequence in a unique object. This unique object is called an
interned string
object
.
3.
You can get the length of a string by invoking its
length()
method, retrieve a char-
acter at the specified
index
in the string using the
charAt(index)
method, and use
the
indexOf
and
lastIndexOf
methods to find a character or a substring in a
string.