Docsity

Parameterized - Data Structures and Advanced Programming - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Parameterized - Data Structures and Advanced Programming - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

CS61B Lecture

Today: Java support for generic programming

Readings for today: Head First Java chapter 16, Assorted Readings on Java chapter 5.

Next topic: Data Structures into Java §6.2, §6.4, §6.5.

The Old Days

  • Java library types such as List didn’t used to be parameterized. All

Lists were lists of Objects.

  • So you’d write things like this:

for (int i = 0; i < L.size (); i += 1) { { String s = (String) L.get (i); ... }

  • That is, must explicitly cast result of L.get (i) to let the compiler know what it is.
  • Also, when calling L.add(x), was no check that you put only Strings into it.
  • So, newest release attempts to alleviate these perceived problems

by introducing parameterized types, like List.

  • Unfortunately, it is not as simple as one might think.

Parameters on Methods

  • Functions (methods) may also be parameterized by type. Example of

use from java.util.Collections:

/** A read-only list containing just ITEM. */ static List singleton (T item) { ... }

In this case, compiler figures out T without help when you call

singleton(x) by looking at the type of x.

  • Another example (from java.util.Collections):

/** An unmodifiable empty list. */ static List emptyList () { ... }

Here, a call to emptyList() would not contain enough information,

so instead we write, e.g., Collections.emptySet (), to

tell the compiler that T is Particle.

Type Bounds

  • Sometimes, your program needs to ensure that a particular type pa- rameter is replaced only by a subtype (or supertype) of a particular type (sort of like specifying the “type of a type.”).
  • For example,

class NumericSet extends HashSet { /** My minimal element */ T min () { ... } ... }

Requires that all type parameters to NumbericSet must be subtypes

of Number (the “type bound”). (T can extend or implement the bound,

as appropriate.

  • Another example:

/** Set all elements of L to X. */ static void fill (List<? super T> L, T x) { ... }

means that L can be a List as long as T is a subtype of (extends

or implements) Q.

Dirty Secrets Behind the Scenes

  • Java’s design for parameterized types was constrained by a desire for backward compatibility.
  • Actually, when you write

class Foo { T x; Foo q = new Foo(); T mogrify (T y) { ... } Integer r = q.mogrify (s); } Java gives really gives you class Foo { Object x; Foo q = new Foo(); Object mogrify (Object y) { ... } Integer r = } (Integer) q.mogrify ((Integer) s); That is, it supplies the casts automatically, and also throws in some additional checks. If it can’t guarantee that all those casts will work, gives you a warning about “unsafe” constructs.

Limitations

Because of Java’s design choices, are some limitations to generic pro- gramming:

  • Since all kinds of Foo or List are really the same,
    • L instanceof List will be true when L is a List.
    • Inside, e.g., class Foo, you cannot write new T (), new T[], or x

instanceof T.

  • Primitive types are not allowed as type parameters.
    • Can’t have ArrayList, just ArrayList.
    • Fortunately, automatic boxing and unboxing makes this substitu- tion easy: int sum (ArrayList L) { int N; N = 0; for (int x : L) { N += x; } return N; }
    • Unfortunately, boxing/unboxing have significant costs.