We noted earlier that Java primitive
type numbers are not class objects. The language designers decided
that the higher processing speed and memory efficiency of simple,
non-class structures for such heavily used data types overruled
the elegance of a pure object-only language.
The designers decided instead that for each primitive
type there would be a corresponding wrapper class. An instance
of a wrapper contains, or wraps, a primitive value of the
corresponding type.
Wrappers allow for situations where numerical values
are needed but objects instead of primitives are required. For example,
a very useful tool is as the Vector
class (see Chapter 11),
which is a list that that can grow or shrink, unlike an array. However,
the elements of a Vector
instance are object references. So if one wants to use a Vector
to hold a list of numbers, the numbers must be wrapped in instances
of their corresponding type.
The wrapper classes also provide various tools such
as constants (the smallest and largest possible int
value, for example) and static methods. You will often use wrapper
methods to convert a number type value to a string or a string to
a number type (see below)
This table lists the primitive types and the corresponding
wrapper classes:
Primitive |
Wrapper |
boolean |
java.lang.Boolean |
byte |
java.lang.Byte |
char |
java.lang.Character |
double |
java.lang.Double |
float |
java.lang.Float |
int |
java.lang.Integer |
long |
java.lang.Long |
short |
java.lang.Short |
void |
java.lang.Void |
See the Java
2 Platform API Specification for listings and descriptions
of the methods and constants provided with each of the wrapper
classes.
Other than for int,
the wrappers come with the same name as the corresponding primitive
type except that the first letter is capitalized. The wrappers
are normal classes that extend the Object
superclass like all Java classes. (We discuss class inheritance
in Chapter 4.)
The wrapper constuctors create class objects from the primitive
types. For example, for a double floating point number "d"
:
double
d = 5.0;
Double aD = new Double(d);
Here a Double
wrapper object is created by passing the double
value in the Double
constructor argument.
In turn, each wrapper provides a method to return the primitive
value
double
r = aD.doubleValue();
Each wrapper has a similar method to access the primitive value:
integerValue()
for Integer,
booleanValue()
for Boolean,
and so forth.
In the section Autoboxing/Unboxing,
we discuss new capabilities added with Java 5.0 for converting
back and forth between a wrapper and its corresponding primitive.
Strings,
Wrappers and Primitives
The primitive type wrappers provide various static utility
methods such as those to convert numbers to strings and vice
versa.
We occasionally want to pass values to an applet. The applet
hypertext tag includes the param
sub-tag for this as shown:
<applet
...>
<param name = "string_name"
value = "string_value" >
</applet>
for passing strings to the applet program. The parameter has
a name and corresponding value. For example, we could pass two
strings with numbers as follows:
<applet
code="myApplet" codebase="Java" width=100
height=50>
<param name="fpNumber" value="12.45">
<param name="intNumber" value="10">
</applet> |
In the applet code, usually in the init()
method, we then use the getParameter(String)
method from the Applet
class to obtain a given parameter, as in
String
fpStr = getParameter ("fpNumber");
String intStr = getParameter ("intNumber");
We now have the parameter values in string form and
need to conert them to numbers. The wrapper classes provide tools
for this in the form of static methods.
For example, the following code:
public
void init () {
string fpStr= getParameter
("fpNumber");
double fpNum = Double.parseDouble
(fpStr);
String intStr = getParameter
("intNumber");
int intNum = Integer.parseInt
(intStr);
....
|
shows the use of the parseDouble
method in Double
and the parseInt
method in Integer.
The parseDouble()
method appeared with Java
1.2. Previously, the valueOf()
method was used to return a
Double
value, which in turn could provide the
double
primitive value using the
doubleValue()
method.
double
fpNum = Double.valueOf(fpStr).doubleValue();
Note that this code demonstrates the common Java technique of executing
several commands in a single line. The line executes left to right.
The
valueOf
method returns a Double
object, which then has its doubleValue()
method called.
Going in the other direction, you can convert a primitive type
to a string in several ways. The String
class provides static valueOf
methods as well as the overloaded "+"
operator.
For example, in this code we first convert numerical values to
strings using the String
class's valueOf()
methods (there is one for each primitive type) and then using the
"+"
operator:
double
d = 5.0;
int i = 1;
String dStr = String.valueOf
(d);
String iStr = String.valueOf
(i);
String aStr = "d = "
+ d;
String bStr = "i = "
+ i; |
Now dStr
and iStr
objects hold string representations of 1 and 5.0, while aStr
holds "d
= 5.0" and bStr
holds "i
= 1".
Demo 6: Wrappers gives
an applet demonstration of converting applet tag parameters to
primitive values.
We discuss string formatting of numerical values such as with decimal
and scientific notation in the Chapter
5: Tech section.
References and Web Resources
Latest update: Oct. 20, 2004
|