Object Wrappers for Primitives

Each of the eight primitive types we have just seen has a corresponding class type, predefined in the Java library. For example, there is a class java.lang.Integer that corresponds to primitive type int. These class types accompanying the primitive types are known as object wrappers and they serve several purposes:

  • The class is a convenient place to store constants like the biggest and smallest values the primitive type can store.

  • The class also has methods that can convert both ways between primitive values of each type and printable Strings. Some wrapper classes have additional utility methods.

  • Some data structure library classes only operate on objects, not primitive variables. The object wrappers provide a convenient way to convert a primitive into the equivalent object, so it can be processed by these data structure classes. One example of a data structure class that only operates on objects is the class java.util.SortedSet, which keeps a collection of objects in sorted order for you.

Table 3-1 shows the names of the wrapper classes for primitive types.

Table 3-1. Object wrapper classes for primitive types

Primitive type

Corresponding wrapper class

boolean

java.lang.Boolean

char

java.lang.Character

int

java.lang.Integer

long

java.lang.Long

byte

java.lang.Byte

short

java.lang.Short

double

java.lang.Double

float

java.lang.Float

These wrapper classes are all found in package java.lang. You have to tell the compiler where to find most of the packages you use, but java.lang is an exception to this rule. The classes in package java.lang are regarded as the most fundamental, and the compiler makes them available to every compilation automatically. You don't have to give the full name java.lang.Byte in your code; it's enough to write Byte.

The names follow the Java convention that class names should start with a capital letter, and method, object, and field names should start with a lowercase letter, to help you tell them apart when reading code. The names Integer and Character do not mirror exactly the names of their primitive type counterparts (int and char). There's no good reason for that, just sloppy code review by the original implementors.

Here's an example of how the wrappers for primitive types might be used. This example shows some methods of Integer. Similar methods exist for the other wrapper classes.

int i = 15;
Integer myInt = new Integer(i); //wrap an int in a object

// get the printable representation of an Integer:
String s = myInt.toString();

// gets the Integer as a printable hex string
String s = myInt.toHexString(15); // s is now "f"

// reads a string, and gives you back an int
i = myInt.parseInt( "2047" );

// reads a string, and gives you back an Integer object
myInt = myInt.valueOf( "2047" );

To see the full capabilities of the wrapper classes, peruse them in the HTML documentation of the Java API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.179.100