Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Java generics

No description

James Shaw

on 6 May 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Java generics

What are generics? Another dimension of type safety List monarchs = new JColorChooser() Before generics List monarchs = new ArrayList() monarchs.add(new Monarch("George I")) monarchs.add(new PrimeMinister("Sir Walpole")) After generics List<Monarch> monarchs = new ArrayList<Monarch>() monarchs.add(new Monarch("William IV")) monarchs.add(new PrimeMinister("Sir Walpole")) Silly Compiler Cat! Compiler Cat says No. Clever Compiler Cat! Before generics After generics Sir Walpole is not a monarch. Sir Walpole is not a monarch. Parameterized type Where can I use generics? Classes Methods class TreeNode<T> {
private final T value, left, right;

public TreeNode(T value, T left, T right) {
this.value = value;
this.left = left;
this.right = right;

public T getValue() { return value; }
public T getLeft() { return left; }
public T getRight() { return right; }
} Think of <T> as an instance field Formal type parameter class TreeNodeFinder {

public <T> T findTreeNodeWithValue(Collection<TreeNode<T>>, T value) {
for (TreeNode<T> node : nodes) {
if (node.getValue().equals(value)) return node;
return null;
} Formal type parameter Return type Think of <T> as a method argument Actual type parameter And introducing Runtime Panda What's are the differences between arrays and generic lists? Arrays Lists Arrays are Covariant Reified String[] is a subtype of Object[] Our type heirarchy Person PrimeMinister Monarch Person[] people = new Monarch[2] people[0] = new Monarch("Edward VII") people[1] = new PrimeMinister("Pitt the Younger") Runtime Panda knows what type of element an array contains ? Runtime Panda Silly Compiler Cat! Person[] people = new Monarch[2]
people[1] = new PrimeMinister("Pitt the Younger") Silly Compiler Cat! ArrayStoreException! Clever Runtime Panda! Generics are Invariant Erased List<String> is not a subtype of List<Object> Only Compiler Cat knows what type of element a List contains List<Person> people = new ArrayList<Monarch>() List<Monarch> monarchs = new ArrayList<Monarch>();
List rawList = monarchs;
List<PrimeMinister> primeMinisters = rawList;

primeMinisters.add(new PrimeMinister("Viscount Goderich"));
Monarch monarch = monarchs.iterator().next(); ClassCastException Wildcards, bounds & type intersection
Type tokens & super type tokens
Erasure, inference & compiler warnings
Type system enhancements in Java 7 & 8 Lots more while (1) {
around.Run() && you.Desert();
lie.Tell() && you.Hurt(); Recapitulation Generics = more type safety

Arrays = covariant & reified
Generics = Invariant & erased

Bounded wildcards = Good Thing. Use them. Raw type What is the average life expectancy of a monarch?

class Main {
Iterable<Monarch> allMonarchs()
Function<Monarch, Integer> timeLived()
Number average(Iterable<Person> people, Function<Person, Number> timeLived)
main() {
Number a = average(allMonarchs(), timeLived());
} Why won't it compile? Function<Monarch, Integer> is not type compatible with Function<Person, Number> but we can make them compatible by using Bounded wildcards Number average(
Iterable<? extends Person> people,
Function<? extends Person, ? extends Number> timeLived) or, even better <P extends Person, N extends Number> N average(
Iterable<P> people,
Function<P, N> timeLived) Iterable<Monarch> is not type compatible with Iterable<Person>
Full transcript