Primitive Variable Wrapper Classes


Primitive variable wrapper classes are aptly named because they are used to wrap a class around a primitive data type value. They are contained in the java.lang package so they are accessible to any program. The importance of primitive variable wrapper classes is evident when you want to pass a primitive value to a method or constructor that takes an Object argument. You cannot, for example, store primitive values in a TreeSet collection, but if you place a wrapper class around the primitive value the wrapper class object can be stored in a TreeSet .

The primitive variable wrapper classes define methods that can be used to convert String objects into wrapper class objects or into primitive values. This can be very useful when reading character data from input files or when a Web-based application such as a Java servlet receives String input arguments from a client request. The input String is parsed into substring elements, and a primitive variable wrapper class method converts the appropriate substring into a primitive value. More details on this process are provided later in this chapter.

Table 15.1. Primitive Variable Wrapper Classes

public final class Boolean extends Object implements Serializable

public final class Byte extends Number implements Serializable

public final class Character extends Object implements Serializable

public final class Double extends Number implements Serializable

public final class Float extends Number implements Serializable

public final class Integer extends Number implements Serializable

public final class Long extends Number implements Serializable

public final class Short extends Number implements Serializable

There is a primitive variable wrapper class for every primitive data type. Table 15.1 lists the declarations for the wrapper classes.

When a class implements the Serializable interface it indicates that an instance of the class can be persistently stored. Usually this means an object can be written to or restored from disk. The Number class is the abstract parent class for the numerical primitive variable wrapper classes.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net