Java Reference
In-Depth Information
Because a is a string,
n
is converted from the integer 7 to the string
Ð7Ñ
. Then the
two strings
ÐAgentÑ
and
Ð7Ñ
are concatenated to form the string
ÐAgent7Ñ
.
Whenever one of the arguments of the + operator is a string, the other argument is
converted to a string.
This concatenation is very useful to reduce the number of
System
,
out.print
instructions. For example, you can combine
System.out.print("The total is ");
System.out.println(total);
to the single call
System.out.println("The total is " + total);
The concatenation
ÐThe total is Ñ + total
computes a single string that
consists of the string
ÐThe total is Ñ
, followed by the string equivalent of the
number
total
.
Sometimes you have a string that contains a number, usually from user input. For
example, suppose that the string variable
input
has the value
Ð19Ñ
. To get the
integer value
19
, you use the
static parseInt
method of the
Integer
class.
int count = Integer.parseInt(input);
//
count is the integer 19
158
159
Figure 3
String Positions
To convert a string containing floating-point digits to its floating-point value, use the
static
parseDouble
method of the
Double
class. For example, suppose
input
is
the string
Ð3.95Ñ
.