Java Reference
In-Depth Information
Integer.toString(x)
However, it is simpler to use the expression "" +x to do the job.
Class Integer also has static methods to get the binary, octal, and hexadec-
imal representations of x as Strings : use the function calls Integer.toBina-
ryString(x) , Integer.toOctalString(x) , and Integer. toHexString (x) .
Finally, class Integer has functions for converting a String to an int . We
look only at method parseInt . Give it a String that represents an integer, and
it converts it to an int :
/** = The integer whose decimal representation is s */
public static int parseInt(String s)
The String representation can begin with a minus sign to indicate a nega-
tive value. But it cannot have any whitespace. The presence of whitespace or of
anything except the decimal representation of an integer results in an exception
and, unless you have prepared for it, termination of the program.
Method parseInt is often used in the following way. A String value is ob-
tained from the input in some manner, say, using our class JLiveWindow . Next,
instance method trim of class String is used to strip away whitespace from
either side of the String . The resulting String , with no whitespace on either
side, is used as an argument to parseInt . Finally, the resulting int value is
assigned to a variable:
d= Integer.parseInt(JLiveWindow.getStringField(0).trim());
5.1.2
Other wrapper classes
Wrapper classes exist in package java.lang for the other primitive types as
well. Here are their names:
Lesson pages
5-1 and 5-2
contain specifi-
cations of the
methods in
these wrapper
classes.
Primitive type Wrapper class
byte Byte
short Short
long Long
float Float
double Double
char Character
boolean Boolean
The wrapper classes for the numerical types and for type boolean are all simi-
lar, and we do not discuss them further here. Look at their API specifications, or
look in Lesson 5 of ProgramLive .
Wrapper class Character has methods that you may find useful later on, so
spend a few minutes looking at them. There are static boolean functions to tell
whether a character is a lower-case character, an upper-case character, a digit, a
letter, a letter or digit, a possible first character of a Java identifier, a possible
Lesson page 5-
2 discusses
wrapper class
Character.
Search WWH ::




Custom Search