INFS 519 - September 14, 2010
Table of Contents
Reading
This lecture covers material from chapters 5.
Generic programming
-
A generic class may be used to generalize a class to support many types
-
IntArrayBag
,FloatArrayBag
,StringArrayBag
, all can be implemented with a singleArrayBag
that treats the contained type as a generic type.
-
-
An
Object
variable can reference any object type.
Widening conversion
- Also known as a covariant conversion
-
Convert to a less constrained type
-
float
->double
-
String
->Object
-
String s = new String("A string object."); Object obj; obj = s;
Narrowing conversion
-
Also know as a
contravariant
conversion -
Convert to a more constrained type
-
float
->int
-
Object
->String
-
Object obj = new String("A string referenced by an Object variable."); String s; s = (String) obj;
Boxing and unboxing
-
Not everything in Java is an object
-
byte
,short
,int
,long
,float
,double
,char
, andboolean
primitives - Integer x = new Integer(3);
- int y = 3;
-
- Each primitive has a wrapper class
Placing a primitive into its wrapper class is boxing and taking a primitive out of its wrapper class is unboxing.
-
Autoboxing and auto-unboxing
- Java will automatically convert (box and unbox) between primitives and their wrapper classes in assignment statements and for method parameters.
int i = 42; int j; Integer example; example = new Integer(i); // Boxing example = i; // Autoboxing j = example.intValue(); // Unboxing j = example; // Auto-unboxing
Object methods
- A method for Integers
static Integer middle(Integer[] data) { if (data.length == 0) { return null; } else { return data[data.length]; } }
-
Change the type to generalize to
Object
-
Must cast using a narrowing conversion to get actual type
-
Integer i
=(Integer) middle(intArray);
-
static Object middle(Object[] data) { if (data.length == 0) { return null; } else { return data[data.length]; } }
Generic methods
- Use a type parameter to make a method generic
static <T> T middle(T[] data) { if (data.length == 0) { return null; } else { return data[data.length]; } }
Generic classes
- A generic class permits the use of type parameters in all methods of the class and for member variables.
public class ArrayBag<E> implements Cloneable { private E[] data; private int manyItems; ... }
-
Using a generic class
-
ArrayBag<Integer> bagOfInts
new ArrayBag<Integer>();
-
-
Review of
ArrayBag
implementation-
Use
equals
method, not == operator (operator compares reference, not value) - Setting of unused array locations to =null=
-
Use
Generic nodes
- Original
public class IntNode { private int data; IntNode link; ... }
- Generic
public class Node<E> { private E data; Node<E> link; ... }
Interfaces
Interfaces may be parameterized by type also.
public interface List<E> { void add(E item); void add(E... items); ... }
Then when defining a class to implement the interface we can specify a type or keep the parameter.
public class IntLinkedList implements List<Integer> { void add(Integer item) { ... } ... }
public class LinkedList<E> implements List<E> { void add(E item) { ... } ... }
HTML generated by org-mode 6.35i in emacs 23