Contents 0 A Roundup of Basic Facts about Generics

advertisement
Contents
0 A Roundup of Basic Facts about Generics
0.1
Type Erasure . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
3
1 Inheritance and Generics
5
2 Programming Project: A Linked List
8
3 Programming Project: An Array-Based List
0
12
A Roundup of Basic Facts about Generics
We have been be writing classes for various data structures, which house collections of objects that are called elements. Each data structure has rules for item
access and for adding items to the collection.
Recall that if we wanted an array list of strings we declared as follows.
ArrayList<String> roster = new ArrayList<String>();
Informally we would say that the object pointed at by roster is an “ArrayList
of strings.” The contents of <..> constitute the type parameter of the ArrayList.
We can use any object type as a type parameter for an ArrayList. We can also
use the wrapper types if we wish to have an array list of a primitive type.
The type ArrayList without a type parameter is called a raw type. When
writing new code, you should not use raw types. We shall see that raw types
can be used.
Let us proceed with an example. Consider this little program.
import java.util.ArrayList;
public class Blank
{
public static void main(String [] args)
{
ArrayList foo = new ArrayList();
foo.add("goo");
}
}
Compilation of this code results in this compiler warning.
1
1 warning found:
File: /Users/morrison/book/javaCode/j10/Blank.java [line: 8]
Warning: /Users/morrison/book/javaCode/j10/Blank.java:8: warning:
[unchecked] unchecked call to add(E) as a member of the raw type
java.util.ArrayList
Java wants to to specify a type parameter for your array list, hence this warning.
Raw types work to ensure that Java is backwards-compatible. Prior to Java 5,
you never specified a type parameter; you only worked with raw types. Objects
stored in an ArrayList were... just Objects.
Now watch what happens when we try to gain access to the contents of our
ArrayList.
import java.util.ArrayList;
public class Blank
{
public static void main(String [] args)
{
ArrayList foo = new ArrayList();
foo.add("goo");
System.out.printf("The length of %s is %d\n", foo.get(0),
foo.get(0).length());
}
}
We get a warning for using a raw type, and then we also get some angry yellow
from the compiler.
1 error and 1 warning found:
------------*** Error ***
------------File: /Users/morrison/book/javaCode/j10/Blank.java [line: 9]
Error: /Users/morrison/book/javaCode/j10/Blank.java:9:
cannot find symbol
symbol : method length()
location: class java.lang.Object
------------** Warning **
------------File: /Users/morrison/book/javaCode/j10/Blank.java [line: 8]
Warning: /Users/morrison/book/javaCode/j10/Blank.java:8: warning:
[unchecked] unchecked call to add(E) as a member of the raw type
2
java.util.ArrayList
Now you might ask why we are getting an error. The call foo.get() returns
an Object, not a String. Therefore foo.get().length() makes no sense; you
cannot compute the length of an Object. You can do this for a String. If you
fetch an object from a container of raw type, you will just get an Object.
What is interesting is that the call to print foo.get(0) will compile and run
(with the warning). Try it! This is because the Object class has a toString()
method. The Object type object foo.get() points an a String with value
"goo". The variable sends the message to the String object and it prints itself.
The Object prints correctly because of the Delegation Principle. The type of
a variable determines what methods are visible. The execution of those methods
is delegated to the object. Since "goo" is a String object, it shows as a string
and not as [email protected]
0.1
Type Erasure
Prior to Java 5, storage in collections such as the ArrayList worked a little like
ObjectOutputStreams. The programmer could add object of any Object type
to an ArrayList. Then, when fetching those values using the get method, the
results had to be cast to the correct type to use them in their original form. In
this system, the programmer is responsible for performing the correct casts.
We see this phenomenon at work in this example.
> import java.util.ArrayList;
> ArrayList foo = new ArrayList();
> foo.add("syzygy");
> foo.add("muffins");
> foo.get(0).length()
Static Error: No method in Object has name ’length’
> ((String) foo.get(0)).length()
6
> System.out.println(foo.get(0))
syzygy
> System.out.println(foo.get(1))
muffins
>
The cast is syntactically clumsy and it is aesthetically execrable. And it was
the old way of doing business.
In modern parlance, you do this instead.
> import java.util.ArrayList;
3
>
>
>
>
6
>
7
>
ArrayList<String> foo = new ArrayList<String>();
foo.add("syzygy");
foo.add("muffins");
foo.get(0).length()
foo.get(1).length()
That ugly cast is now gone. Where’d it go? This is no mere detail. It is
important you understand what is happening behind the scenes, so you fully
understand the benefits, quirks and possible dangers entailed with using generics.
Again, you should always use generics when writing new code. However, it
is important to understand what is happening, because you will see legacy code
that does not use generics, and you need to know how to read and manage it
correctly.
So how does that all work? Suppose you use an ArrayList with a type
parameter. You compile the program. When you do, any calls that add items
to the ArrayList are checked to see if they are adding items of the correct type.
The compiler enforces this contract.
At run time all of these have the same appearance
• ArrayList<Integer>
• ArrayList<ArrayList<Integer>>
• the raw type ArrayList
Any calls that get items from the ArrayList have the appropriate casts
added to them by the compiler. You are given the Cast Iron Guarantee, which
says that all of the necessary casts will be added by the compiler.
Danger! You can void the Cast Iron Guarantee: The existence of an “unchecked
warning” voids the guarantee.
If you are using generic classes, you cannot have an ”unchecked warning;”
this undermines the safety of your entire program. It is the responsibility of the
creator of a generic class to extirpate any of these that might crop up.
When your code is in its run-time form, all evidence of generics is gone. All
this code sees is the casts created by the compiler. Your code at runtime looks
like pre-5 Java code, with raw types. This approach has two important benefits.
Pre-5 Java code will still compile nicely. Java maintains backwards compatibility.
4
This erasure mechanism helps keep your byte code small. All Java ArrayLists
look exactly the same at run time, so there is only one segment of code for
ArrayList.
In contrast, C++ has a similar-looking mechanism called templates. A close
analog to a Java ArrayList is C++’s vector class. If you make vectors with
several different type parameters, object code will be generated for each one.
This phenomenon is sometimes known as “code bloat.” This is the opposite
approach to generic programming to the type erasure approach of Java.
Be warned that there will be some pitfalls for you when writing generic
code, especially if you deal with arrays. We encountered this problem in in the
creation of our array based stack class. Take a look at this little class. In it, we
attempt to create an array of generic type.
public class UglySurprise<T>
{
T[] myArray;
public UglySurprise(int n)
{
myArray = new T[n];
}
}
Now compile and you see an error. Java does not permit the creation of arrays
of generic type.
1 error found:
File: /Users/morrison/book/javaCode/j10/UglySurprise.java [line: 6]
Error: /Users/morrison/book/javaCode/j10/UglySurprise.java:6: i
generic array creation
The reason for this will be revealed when we discuss subtyping and generics.
1
Inheritance and Generics
We will use the term type to refer to a class or an interface. We say S is a subtype
of T for any of these three situations.
• S and T are interfaces and S is a subinterface of T.
• S and T is a class implementing S.
• S is a subclass of T.
If S is a subtype of T we will say that T is a supertype of S. Let us begin by
creating these classes and compiling.
5
public class General
{
}
public class Specific extends General
{
}
import java.util.ArrayList;
public class ArrayTest
{
public static void main(String[] args)
{
Specific [] s = new Specific[10];
General [] g = new General[10];
ArrayList<Specific> as = new ArrayList<Specific>();
ArrayList<General> ag = new ArrayList<General>();
testArray(s);
testArray(g);
testArrayList(as);
testArrayList(ag);
}
public static void testArray(General[] g)
{
}
public static void testArrayList(ArrayList<General> ag)
{
}
}
In these classes, Specific is a subtype of General. All now compiles nicely.
Now go into the main method of the ArrayTest class and add these four lines
of code.
Specific [] s = new Specific[10];
General [] g = new General[10];
testArray(s);
testArray(g);
Your code will still compile. This is because array types are covariant, i.e., that
if S is a subtype of T, then S[] is a subtype of T[]. This seems intuitive and it
does not come as any kind of terrible surprise.
Let us formulate analogous code for an ArrayList.
6
ArrayList<Specific> as = new ArrayList<Specific>();
ArrayList<General> ag = new ArrayList<General>();
testArrayList(as);
testArrayList(ag);
Now compile this. Here is the result.
1 error found:
File: /Users/morrison/book/javaCode/j10/ArrayTest.java [line: 13]
Error: /Users/morrison/book/javaCode/j10/ArrayTest.java:13:
testArrayList(java.util.ArrayList<General>) in ArrayTest cannot be
applied to (java.util.ArrayList<Specific>)
Why the difference? This is because generics are invariant; i.e., if S is a
subtype of T, then ArrayList<S> is not a subtype of T unless S and T are in
fact the same type.
In java.util there is an interface called List. The standard ArrayList
and LinkedList classes in java.util implement List so it is possible for List
variables to point at ArrayList or LinkedList objects.
Let us learn what subtype relationships occur here. Modify ArrayTest.java
as follows.
import java.util.ArrayList;
import java.util.List;
public class ArrayTest
{
public static void main(String[] args)
{
Specific [] s = new Specific[10];
General [] g = new General[10];
ArrayList<Specific> as = new ArrayList<Specific>();
ArrayList<General> ag = new ArrayList<General>();
}
public static void testArray(General[] g)
{
}
public static void testArrayList(ArrayList<General> ag)
{
}
}
We now perform an experiment by adding this code to the main method.
7
List<General> lg = new ArrayList<General>();
This does compile. If Foo is a subtype of Goo and these two classes are generic,
then Foo<T> is a subtype of Goo<T>. Notice that this breaks things.
List<General> lgs = new ArrayList<Specific>();
We show the resulting compiler error.
1 error found:
File: /Users/morrison/book/javaCode/j10/ArrayTest.java [line: 13]
Error: /Users/morrison/book/javaCode/j10/ArrayTest.java:13:
incompatible types
found
: java.util.ArrayList<Specific>
required: java.util.List<General>
Notice that a ArrayList<General> is a subtype of List<General>, but things
break because of invariance. An ArrayList<Specific> is not a subtype of
ArrayList<General>
2
Programming Project: A Linked List
In this project, you will write a simplified version of Java’s LinkedList class.
The purpose of this project is to acquaint you further with link-bases structures
and for you to get your hands dirty with them.
Here is the interface.
8
Header
LinkedList<E>()
boolean addLast(E
newItem)
boolean addFirst(E
newItem)
boolean add(int
index, E newItem)
void clear()
boolean
contains(Object
o)
int size()
E get(int n)
E set(int n, E
newItem)
int indexOf(Object
o)
int
lastIndexOf(Object
o)
boolean
remove(Object o)
Action
This constructor creates an empty linked
list.
This places the new item on the end of the
list. Return true if the item is successfully
added.
This places the new item at the beginning
of the list. Return true if the item is successfully added
This
inserts
newItem
into
the
list at the prescribed index.
An
IndexOutOfBoundsException is thrown if
an illegal index is used.
This empties the linked list of its items.
This returns true if the object o is present
in the list.
This returns the number of objects present
in the list.
This returns the object in position n. It
throws an IndexOutOfBoundsException if
you try to return an nonexistent element
This replaces the item at index n
with item newItem.
It throws an
IndexOutOfBoundsException if you try to
return an nonexistent element
This returns the index of the first instance
of o in the list, or -1 if the object does not
appear in the list.
This returns the index of the last instance
of o in the list, or -1 if the object does not
appear in the list.
This removes the object o from the list if
it is present. It returns true if the list contains the object.
We provide you with a shell for the program.
import java.util.Iterator;
class LinkedList<E> implements Iterable<E>
{
private Link<E> head;
public LinkedList()
{
head = null;
}
9
public boolean isEmpty()
{
return head == null;
}
public void addFirst(E newItem)
{
//look at the link based stack
}
public void seymour()
{
}
public boolean contains(Object o)
{
}
/***********Iteraor related stuff
public Iterator<E> iterator()
{
return null;
}
class Navigator implements Iterator<E>
{
//add the necessary methods
}
}
class Link<E>
{
private E datum;
private Link<E> next;
public Link(E _datum, Link<E> _next)
{
datum = _datum;
next = _next;
}
public Link(E _datum)
{
this(_datum, null);
}
public E getDatum()
{
return datum;
}
public Link<E> getNext()
{
return next;
}
public boolean hasNext()
10
{
return next != null;
}
public void setNext(Link<E> newNext)
{
next = newNext;
}
public void setDatum(E newDatum)
{
datum = newDatum;
}
}
11
3
Programming Project: An Array-Based List
Header
ArrayList<E>(int
capacity)
ArrayList<E>()
boolean addLast(E
newItem)
boolean addFirst(E
newItem)
boolean add(int
index, E newItem)
void clear()
boolean
contains(Object
o)
int size()
E get(int n)
E set(int n, E
newItem)
int indexOf(Object
o)
int
lastIndexOf(Object
o)
boolean
remove(Object o)
Action
This constructor creates an empty Array list with the specified capacity. An
IllegalArgumentException is thrown if
the client passes a non-positive integer.
This constructor creates an empty Array
list with a capacity of 10.
This places the new item on the end of the
list. Return true if the item is successfully
added.
This places the new item at the beginning
of the list. Return true if the item is successfully added
This
inserts
newItem
into
the
list at the prescribed index.
An
IndexOutOfBoundsException is thrown if
an illegal index is used.
This empties the linked list of its items.
This returns true if the object o is present
in the list.
This returns the number of objects present
in the list.
This returns the object in position n. It
throws an IndexOutOfBoundsException if
you try to return an nonexistent element
This replaces the item at index n
with item newItem.
It throws an
IndexOutOfBoundsException if you try to
return an nonexistent element
This returns the index of the first instance
of o in the list, or -1 if the object does not
appear in the list.
This returns the index of the last instance
of o in the list, or -1 if the object does not
appear in the list.
This removes the object o from the list if
it is present. It returns true if the list contains the object.
We provide you with a shell for the program.
import java.util.Iterator;
12
class ArrayList<E> implements Iterable<E>
{
private int capacity;
private int size;
private Object[] entries;
public ArrayList(int _capacity)
{
capacity = _capacity;
size = 0;
entries = new Object[capacity];
}
public ArrayList()
{
this(10);
}
public boolean isEmpty()
{
}
public void addFirst(E newItem)
{
//look at the array based stack
}
@Override
public String toString()
{
return "";
}
public boolean contains(Object o)
{
return false;
}
/***********Iteraor related stuff
public Iterator<E> iterator()
{
return null;
}
class Navigator implements Iterator<E>
{
//add the necessary methods
}
}
13