Creating Primitive Variable Wrapper Class Objects


There are several ways to create an instance of a primitive variable wrapper class. The first way is to use one of the constructors provided by the classes. The constructors for each class are shown in Table 15.2. Each class provides a constructor that takes its corresponding primitive type as an input parameter. This is the value that the wrapper class wraps. Every class except Character also defines a constructor that takes a String representation of a primitive data type value as its input parameter. If the String passed to the Boolean constructor is not true (ignoring case) the resulting Boolean object will represent the value false . The numerical wrapper class constructors will throw a NumberFormatException if the String does not represent a parsable numerical value. The Float class defines a third constructor that takes a primitive double value as an argument.

Table 15.2. Primitive Variable Wrapper Class Constructors

C LASS

C ONSTRUCTOR

Boolean

public Boolean(boolean value)

public Boolean(String value)

Byte

public Byte(byte value)

public Byte(String value)

Character

public Character(char value)

Double

public Double(double value)

public Double(String value)

Float

public Float(float value)

public Float(double value)

public Float(String value)

Integer

public Integer(int value)

public Integer(String value)

Long

public Long(long value)

public Long(String value)

Short

public Short(short value)

public Short(String value)

Another way to obtain a primitive variable wrapper class object is by using one of the valueOf() methods defined in each class. These are static methods so you don't need to create a wrapper class object to use them. Similar to the constructors, the valueOf() methods can take either a primitive value or a String representation of a primitive value as an argument. The declarations for these methods are shown in Table 15.3.

An important difference between the valueOf() methods and the constructors is that other than one version defined in the Boolean class, the valueOf() methods do not take a primitive value as an input argument. The methods require a String representation of a primitive value. The Byte , Integer , Long , and Short classes provide versions of valueOf() that let you specify the base of the number system being used by setting the radix value. The default value of radix is 10, which corresponds to the decimal system. The valueOf() methods in the numerical wrapper classes will throw a NumberFormatException if the String is not a proper representation of a number.

Table 15.3. Static Instance Creation Methods

C LASS

M ETHOD

Boolean

public static Boolean valueOf(boolean value)

public static Boolean valueOf(String value)

Byte

public static Byte valueOf(String value)

public static Byte valueOf(String value, int radix)

Double

public static Double valueOf(String value)

Float

public static Float valueOf(String value)

Integer

public static Integer valueOf(String value)

public static Integer valueOf(String value, int radix)

Long

public static Long valueOf(String value)

public static Long valueOf(String value, int radix)

Short

public static Short valueOf(String value)

public static Short valueOf(String value, int radix)

One final note about primitive variable wrapper class objects ” they are immutable. Once a primitive variable wrapper class object is created, the primitive value associated with it cannot be changed.

Example: Using Primitive Variable Wrapper Classes

In the CreateWrapper class, we want to store some information about a rocket engine configuration in a HashMap . The data includes int and double primitive type values and a String . A HashMap can only store objects, so the primitive variable wrapper classes Integer and Double are used to wrap the primitive values

In the second part of the example, the get() method is used to retrieve the value of the fuel density key in the HashMap . The get() method returns an Object that is cast into a Double . The Double object's doubleValue() method can then be called that returns the primitive value associated with the Double . The doubleValue() method is described in the next section.

 import java.util.HashMap; public class CreateWrapper {   public static void main(String args[]) {     //  Some data about an engine configuration is     //  created as well as a HashMap object.     String name = "SSME";     int numberOfEngines = 4;     double fuelDensity = 4.43;     double oxidizerDensity = 71.3;     HashMap engineData = new HashMap();     //  Data is added to the HashMap. A HashMap can only     //  store Objects, so primitive variable wrapper     //  classes are used to wrap the integer and     //  floating point values.     engineData.put("engine type", name);     engineData.put("engine number",                     new Integer(numberOfEngines));     engineData.put("fuel density",                     new Double(fuelDensity));     engineData.put("oxidizer density",                     new Double(oxidizerDensity));     //  The get() method returns the value associated     //  with a specified key in the HashMap.  The return     //  value is an Object which is cast into a Double,     //  on which the doubleValue() method is called to     //  convert it to the corresponding primitive value     double value = ((Double)engineData.get("fuel density")).doubleValue();     System.out.println("fuel density is "+value);   } } 

Output ”

 fuel density is 4.43 


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