Return to the Lecture Notes Index

15-200 Lecture 4 (Thursday, May 19, 2005)

Wrapper Classes

As you have already learned, Java has two different kinds of data types: primitives and objects. The primitive types include int (for integers), short (for small integers), long (for large integers), float (for real-ish numbers), double (for higher-precision or larger real-ish numbers), char (for characters), byte (for small, 1-byte values), and boolean (for true/false values). These are simple pieces of data.

Objects can combine both data and methods, and sometimes it is more convenient to treat primitive values as objects. How can we do this? Java provides wrapper classes which create objects that store a primitive value and contain methods for working with the primitive value.

The wrapper classes are named using the following convention: take the name of the primitive type and capitalize the first letter. For example, the primitive type int is for integers, so the corresponding wrapper class would be called Integer. The wrapper class for float would be Float, for char would be Character, etc.

To create an object of a wrapper class, we would do something like:

int x = 5;
Integer i;
i = new Integer(x);

To get the primitive value back from a wrapper class, we would do:

int x;
Integer i;
i = new Integer(5);
x = i.intValue(); // returns the value stored in the object

Arrays And Vectors

Suppose we want to store a group of related values together. Java provides two options: arrays and vectors .

An array is a fixed-length block of memory of some type. You could have an array of 8 integers, or an array of 1000 doubles, or even an array of 53 references to objects. In an array, the length cannot change, and every element of the array must be of the same type.

A vector in like an array, only it has much more flexibility. A vector starts out with some length, and then it grows itself whenever necessary. To do the equivalent with an array would require us to create a new array and copy over all of the values from the old array. The "Vector" class takes care of this for us.

One disadvantage of vectors is that they cannot hold primitive values. They can hold any kind of object, however, so we can use the wrapper classes in order to put primitive data into a vector.

The "Vector" Class

You can look at the full documentation of the Vector class (java.util.Vector) in the Java API documentation, but here is a quick description of some of its methods:

The addElement() method takes something of type Object as a parameter, and the elementAt() method returns a value of type Object, but what exactly is the Object type? Without going into the details right now, every class in Java is a type of Object. In other words, unless specifically overridden, all objects inherit the properties of the Object class -- these inherited properties are there by default.

Enumerations

An enumeration provides us with a convenient way to traverse a collection of data. Enumeration is not exactly a class, but for now you can think of it as one. Enumeration is actually an interface, which is a specification for the implementation of a class. Many collections in Java implement the Enumeration interface. (Aside: But nto as many as one would hope).

An enumeration put the elements of a collection into some sequential order, and then steps through them one at a time. While traversing an enumeration, we can never go back to a previous value; once we reach the end can get no more data from it.

The two methods of the Enumeration interface are:

We can enumerate a vector and traverse it as follows:


Vector v = new Vector();
// do a bunch of stuff with Vector

Enumeration e = v.elements();

while (e.hasMoreElements())
{
String s = (String) e.nextElement();
System.out.println(s);
}

Why would we want to use an enumeration? Why not just step through the elements of the vector one at a time and process them that way? The Enumeration interface gives us a common way to traverse any collection of data. Suppose we want to print a collection of data, but we don't know what kind of data structure we are going to use. As long as the structure implements the Enumeration interface, we know that it will have the hasMoreElements() and nextElement() methods, so we can write a print routine that doesn't depend on the actual structure. We do have to be careful when using enumerations, though, because we do not know what order the elements will come out in.