Programming Languages (CS302 2006S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[CSC302 1999S]
Back to Class Cancelled. On to Beyond Java.
Held: Wednesday, March 15, 2006
Summary: Today we consider Java's generics. Introduced in Java 1.5, generics permit you to parameterize class and interface declarations.
Related Pages:
Assignments
Notes:
Overview:
featuresis that it does relatively strong compile-time type checking to limit the number of silly errors programmers make.
stuff
only contains String values, we still need to cast
any value coming out of it.
public String smallestString(Vector stuff)
{
String guess = (String) stuff.get(0);
int len = stuff.size();
for (int i = 1; i < len; i++) {
String candidate = (String) stuff.get(i);
if (guess.compareTo(candidate) > 0)
guess = candidate;
} // for
return guess;
} // smallestString(Vector)
smallestString with an incorrect vector.
public interface LinearStructure<T>
{
public void put(T val);
public T get();
public boolean isEmpty();
} // interface LinearStructure<T>
Vector<String> names = new Vector<String>();
smallestString as
public String smallestString(Vector<String> stuff)
{
String guess = stuff.get(0);
int len = stuff.size();
for (int i = 1; i < len; i++) {
String candidate = stuff.get(i);
if (guess.compareTo(candidate) > 0)
guess = candidate;
} // for
return guess;
} // smallestString(Vector<String>)
public class Point<T>
{
T x;
T y;
...
} // class Point<T>
extends Type.
public class Point<T extends Number>
B
is a subclass of A. Which of the following assignments
should we be able to do, and why or why not?
A a = ...; B b = ...; Vector<A> va = ...; Vector<B> vb = ...; a = b; // Valid? b = a; // Valid? va = vb; // Valid? vb = va; // Valid?
a = b is acceptable.
va = vb acceptable? Because when we try
to call va.put(...), the type checker only knows that
va contains A's. However, for type safety, we need
to ensure that only B's.
smallestString generically to see those
complications.
Object (or the specified superclass/interface)
for each type parameter.
Back to Class Cancelled. On to Beyond Java.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[CSC302 1999S]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Wed May 10 09:02:56 2006.
The source to the document was last modified on Thu Jan 12 09:00:38 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS302/2006S/Outlines/outline.23.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby