CS 1723-001 Data Structures
Fall 2001 -- Lectures for Week 2


Generic Code in Java:
How to Get the Same Code to Work for Different Types

Week's Objectives Related to Lists:
  1. Study an improved version of last week's List example -- one that implements an interface.
  2. Look at wrapper classes briefly.
  3. Look at the Comparable interface.
  4. Rewrite the code for the list of integers using the Object and Comparable classes. At the end we still have a list of integers with the same functionality.
  5. Show how to use exactly the same code to work with other types, such as double or String.
  6. Show how to use exactly the same code to work with some user-defined type.

Details About Lists:

  1. interfaces. An interface is just a list of methods (along with possibly other things such as constants) without the implementation of those methods. A class can implement an interface in any way it wants, by supplying code for every method of the interface. Here is an interface (List.java) for a more complicated version of the List example along with the implementation of the interface for integers (IntList.java) and with all the other necessary classes and with a sample run: list using an interface. Notice that this List has new methods removeMax and removeMin that depend on the natural order of integers, as tested by the relational operators: < > <= >=.

  2. wrapper classes. Recall that Java has the primitive types: boolean, char, byte, short, int, long, float, and double.

    These types are not treated as objects in Java and are represented by their bit representations. All other types are objects, represented by a reference to them, that is, by a machine address or a pointer. Each primitive type has a corresponding wrapper class that will hold the type as an object with a reference to it. The wrapper classes for primitive types are: Boolean, Character, Byte, Short, Integer, Long, Float, and Double.

    For example, the Integer type holds an int value, but it is an object with named constants and methods, and it can be handled like other objects.

    If we have a int, such as int num = 47;, this can be converted to Integer type using

    
         Integer numWrap = new Integer(num);
    
    Given an Integer such as numWrap above (just an arbitrary name), there are various methods that can be used: numWrap.valueOf() returns the value 47. The method numWrap.toSting() converts the value to a String (though this can be done simply by numWrap + ""). More importantly, there is a method that corresponds to the comparison operators: for two variables num1Wrap and num2Wrap of type Integer, one can write
    
         num1Wrap.compareTo(num2Wrap)
    
    The result is > 0 in case num1 > num2, is < 0 in case num1 < num2, and == 0 in case num1 == num2, where num1 and num2 are the ints corresponding to num1Wrap and num2Wrap.

    Given an int inside an Integer wrapper class, it can be handled like other objects.

  3. The Comparable interface. Here is the standard code for this interface:
        public interface Comparable {
           public int compareTo(Object x);
        }
    
    A class implements Comparable by supplying code for this compareTo method. Note that obj1.compareTo(obj2) < 0 means that obj1 is less than obj2. Similarly, obj1.compareTo(obj2) > 0 means that obj1 is greater than obj2, and obj1.compareTo(obj2) == 0 means that obj1 is equal to obj2. The Comparable interface is already implemented for the wrapper classes of primitive types as well as for String, so that Comparable is available for these classes.

  4. The List class rewitten for ints. This is part of the material available under the next item.

  5. Using the same List code for other types. here.

    I have created a sheet comparing the code for the original version of the List class (the one with an interface, but hardwired for integers) with the version that uses Comparable types: In Postscript    In PDF

    Here is another sheet comparing the four different main methods in the examples above: In Postscript    In PDF

  6. As a final example, I used the Rational class from CS 1713 that supports rational numbers (fractions). To be used in a list, this class needs to implement Comparable (so that the string "implements Comparable" must appear in the class declaration, and code for a compareTo method must be supplied). For this application, Rational also needs a toString method. Finally, I put a getNextRational method into the GetData class. The list classes List and CompList were exactly the same as before. (Not a single character changed.) The main method and its class were almost exactly the same. list of rationals.

Revision date: 2001-08-31. (Please use ISO 8601, the International Standard.)