Programming Languages (CS302 2007S)
[Skip to Body]
Admin:
[Front Door]
[Glance]
[Handouts]
[Honesty]
Current:
[Current Outline]
[Current EBoard]
[Current Homework]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Outlines]
[Readings]
[Reference]
[HOG]
Misc:
[SamR]
[CSC302 1999S]
[CSC302 2006S]
This outline is also available in PDF.
Held: Friday, March 16, 2007
Summary: Today we consider Java's generics. Introduced in Java 1.5, generics permit you to parameterize class and interface declarations.
Related Pages:
Due
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.
Vector vec = new Vector(); vec.add(new BigInteger(23)); System.out.println(smallestString(vec));
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 we can only add B
's.
smallestString
generically to see those
complications.
Object
(or the specified superclass/interface)
for each type parameter.
[Skip to Body]
Admin:
[Front Door]
[Glance]
[Handouts]
[Honesty]
Current:
[Current Outline]
[Current EBoard]
[Current Homework]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Outlines]
[Readings]
[Reference]
[HOG]
Misc:
[SamR]
[CSC302 1999S]
[CSC302 2006S]
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 Sun Apr 29 11:26:26 2007.
The source to the document was last modified on Sat Jan 20 21:34:53 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS302/2007S/Outlines/outline.24.html
.
You may wish to validate this document's HTML ; ; Check with Bobby
Samuel A. Rebelsky, rebelsky@grinnell.edu