Wrapper Classes

     

You can't call a method on a Java primitive, such as an int or a double . And sometimes that's a problem because you would like to be able to. Sometimes, you need to hold a list of these kinds of values, and you want to use the classes in the Java Collections Framework; the problem here is that none of those data structures, such as ArrayList, Hashtable, or Vector, will hold primitive types. They only hold objects.

So what's a girl to do? Use the wrapper classes that come with the Java API. There is a class in the java.lang package that corresponds to each of the primitive types. Table 11-1 shows these classes.

The Primitive Wrapper Classes

W RAPPER

P RIMITIVE

Boolean

boolean

Byte

byte

Short

short

Character

char

Integer

int

Long

long

Float

float

Double

double


The preceding wrapper classes allow their corresponding primitive types to be used as objects. They provide the following methods that make working with them easy and fun. The wrapper classes share these methods in general. Let's take one wrapper, Boolean, as an example, from which to extrapolate.

First, the constructors you can use to make Boolean wrappers:

  • public Boolean(String b) Creates a Boolean object representing the value true if the argument is "true" (ignoring case).

  • public Boolean(boolean b) Creates a Boolean object corresponding to the value of the passed Boolean primitive.

  • boolean booleanValue() Call this method on an object of type Boolean when you want to get its value as a primitive. The corresponding methods in the other classes include intValue() , doubleValue() , and so forth.

  • public int compareTo() Compares the values of two wrappers of the same type. It returns 0 if this object represents the same primitive value as the argument.

  • public static boolean parseBoolean(String s) Takes a String argument and returns its Boolean value.

  • static Boolean valueOf(boolean b) Returns an instance of the Boolean object corresponding to the boolean argument. This method is the sister of booleanValue() , which you use to get a primitive from a wrapper ”the xValueOf() methods get a wrapper from a primitive. This method is overloaded to accept a String argument as well.

Boolean has three static fields: TRUE and FALSE , which represent their primitive counterparts, and TYPE , which represents the primitive type boolean.

I only show the Boolean wrapper class here as an example, because the other wrapper classes work very similarly, and share similar methods. The Integer wrapper class, for example, features an intValue method, and the Long wrapper class features a longValue method.

The wrappers are necessary when you need to use your primitive values with some API that only will accept working with objects. However, because that kind of functionality is so fequently needed in object-oriented programming, a new feature was added to Java 5.0; autoboxing, which we discuss next .



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

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