Java Reference
In-Depth Information
returns the
double
value
199.98
. If there is any possibility that the string named by
theString
has extra leading or trailing blanks, you should instead use
Double.parseDouble(theString.trim())
The method
trim
is a method in the class
String
that trims off leading and trailing
whitespace, such as blanks.
If the string is not a correctly formed numeral, then the invocation of
Double.parseDouble
will cause your program to end. The use of
trim
helps somewhat
in avoiding this problem.
Similarly, the static methods
Integer.parseInt
,
Long.parseLong
, and
Float.parseFloat
convert from string representations to numbers of the correspond-
ing primitive types
int
,
long
, and
float
, respectively.
Each of the numeric wrapper classes also has a static method called
toString
that
converts in the other direction, from a numeric value to a string representation of the
numeric value. For example,
parseInt
Double.toString(123.99)
returns the string value
"123.99
".
Character
, the wrapper class for the primitive type
char
, contains a number of static
methods that are useful for string processing. Some of these methods are shown in Display
5.8. A simple example of using the static method
toUpperCase
of the class
Character
is
given in Display 5.9. As is typical, this program combines the string-processing methods of
the class
String
with the character-processing methods in the class
Character
.
There is also a wrapper class
Boolean
corresponding to the primitive type
boolean
.
It has names for two constants of type
Boolean
:
Boolean.TRUE
and
Boolean.FALSE
,
which are the
Boolean
objects corresponding to the values
true
and
false
of the
primitive type
boolean
.
Character
Boolean
Display 5.8
Some Methods in the Class
Character
(part 1 of 2)
The class
Character
is in the
java.lang
package, so it requires no
import
statement.
public static char
toUpperCase(
char
argument)
Returns the uppercase version of its
argument
. If the
argument
is not a letter, it is returned
unchanged.
EXAMPLE
Character.toUpperCase('a')
and
Character.toUpperCase('A')
both return
'A'.
public static char
toLowerCase(
char
argument)
Returns the lowercase version of its
argument
. If the
argument
is not a letter, it is returned unchanged.
EXAMPLE
Character.toLowerCase('a')
and
Character.toLowerCase('A')
both return
'a'.
(continued)