Copyright 2017 - Jacob D. Parr
https://www.jacobparr.com
(all rights reserved)
Coffee<T>
Java Generics
For every code snippet, determine if it's...
Hit the down arrow to continue
Java Generics
Hit the down arrow to continue
List<String> list = new ArrayList<String>();
Java Generics
Hit the down arrow to continue
List<String> list = new ArrayList<String>();
Java Generics
Hit the down arrow to continue
List<String> list = new ArrayList<>();
Java Generics
Hit the down arrow to continue
List<String> list = new ArrayList<>();
Java Generics
Hit the down arrow to continue
List<Object> list = new ArrayList<String>();
Java Generics
Hit the down arrow to continue
List<Object> list = new ArrayList<String>();
Java Generics
Hit the down arrow to continue
ArrayList<Object> list = new ArrayList<String>();
Java Generics
Hit the down arrow to continue
ArrayList<Object> list = new ArrayList<String>();
Java Generics
Hit the down arrow to continue
ArrayList<String> list = new ArrayList<Object>();
Java Generics
Hit the down arrow to continue
ArrayList<String> list = new ArrayList<Object>();
Java Generics
Hit the down arrow to continue
ArrayList list = new ArrayList<String>();
Java Generics
Hit the down arrow to continue
ArrayList list = new ArrayList<String>();
Java Generics
Hit the down arrow to continue
ArrayList<String> list = new ArrayList();
Java Generics
Hit the down arrow to continue
ArrayList<String> list = new ArrayList();
Java Generics
Hit the down arrow to continue
ArrayList<String> stringList = null; ArrayList<? extends String> extdStringList = stringList;
Java Generics
Hit the down arrow to continue
ArrayList<String> stringList = null; ArrayList<? extends String> extdStringList = stringList;
Java Generics
Hit the down arrow to continue
ArrayList<?> list = new ArrayList<Number>();
Java Generics
Hit the down arrow to continue
ArrayList<?> list = new ArrayList<Number>();
Java Generics
Hit the down arrow to continue
ArrayList<? extends Number> extendsList = null; ArrayList<? super Number> superList = extendsList;
Java Generics
Hit the down arrow to continue
ArrayList<? extends Number> extendsList = null; ArrayList<? super Number> superList = extendsList;
Java Generics
Hit the down arrow to continue
ArrayList<? super Number> superNumList = null; ArrayList<? super Integer> superIntList = superNumList;
Java Generics
Hit the down arrow to continue
ArrayList<? super Number> superNumList = null; ArrayList<? super Integer> superIntList = superNumList;
Java Generics
Hit the down arrow to continue
ArrayList<Object> list = new ArrayList<Object>(); list.add(“Hello”);
Java Generics
Hit the down arrow to continue
ArrayList<Object> list = new ArrayList<Object>(); list.add(“Hello”);
Java Generics
Hit the down arrow to continue
ArrayList<?> list = new ArrayList<String>(); list.add(“Hello”);
Java Generics
Hit the down arrow to continue
ArrayList<?> list = new ArrayList<String>(); list.add(“Hello”);
Java Generics
Hit the down arrow to continue
ArrayList<? super Integer> list= new ArrayList<Integer>(); list.add(1);
Java Generics
Hit the down arrow to continue
ArrayList<? super Integer> list= new ArrayList<Integer>(); list.add(1);
Java Generics
Hit the down arrow to continue
ArrayList<? extends Number> list = new ArrayList<Integer>(); list.addAll(new ArrayList<Integer>());
Java Generics
Hit the down arrow to continue
ArrayList<? extends Number> list = new ArrayList<Integer>(); list.addAll(new ArrayList<Integer>());
Java Generics
Hit the down arrow to continue
ArrayList<? super Integer> listA = new ArrayList<Number>(); ArrayList<? extends Integer> listB = new ArrayList<Integer>(); listB.addAll(listA);
Java Generics
Hit the down arrow to continue
ArrayList<? super Integer> listA = new ArrayList<Number>(); ArrayList<? extends Integer> listB = new ArrayList<Integer>(); listB.addAll(listA);
Java Generics
Hit the down arrow to continue
ArrayList<? super Integer> listA = new ArrayList<Number>(); ArrayList<? extends Integer> listB = new ArrayList<Integer>(); listA.addAll(listB);
Java Generics
Hit the down arrow to continue
ArrayList<? super Integer> listA = new ArrayList<Number>(); ArrayList<? extends Integer> listB = new ArrayList<Integer>(); listA.addAll(listB);
Java Generics
Hit the down arrow to continue
ArrayList<Integer> intList = new ArrayList<Integer>(); intList.add(new Integer(1)); ArrayList<? extends Number> extendsNumList = intList; Integer integer = extendsNumList.get(0);
Java Generics
Hit the down arrow to continue
ArrayList<Integer> listA = new ArrayList<Integer>();
listA.add(new Integer(1));
ArrayList<? extends Number> listB = listA;
Integer integer = listB.get(0);
Java Generics
Hit the down arrow to continue
ArrayList<String>[] twoDimArray = new ArrayList<String>[10];
Java Generics
Hit the down arrow to continue
ArrayList<String>[] twoDimArray = new ArrayList<String>[10];
Java Generics
Hit the down arrow to continue
Pair<String, String>[] array = new Pair[2]; array[0] = new Pair<String, String>(“1′′, “y”); array[1] = new Pair(true, false);
Java Generics
Hit the down arrow to continue
Pair<String, String>[] array = new Pair[2]; array[0] = new Pair<String, String>(“1′′, “y”); array[1] = new Pair(true, false);
Java Generics
Hit the down arrow to continue
Pair<?,?>[] array = new Pair<?,?>[2] ; array[0] = new Pair<Integer,Integer>(0,0); array[1] = new Pair<String,String>(“Hello”,”There”);
Java Generics
Hit the down arrow to continue
Pair<?,?>[] array = new Pair<?,?>[2] ; array[0] = new Pair<Integer,Integer>(0,0); array[1] = new Pair<String,String>(“Hello”,”There”);
Java Generics
Create the class Dog
Java Generics
This is our public static void main(..)
Java Generics
Java Generics
Java Generics
Create the class Class
Java Generics
Java Generics
Why doesn't that work?
Java Generics
List<Pet> pets = new ArrayList<Pets>()
List<Dog> dogs = new ArrayList<Dogs>() // Dogs and dogs only !!
addCats(pets);
addCats(dogs); //Compile error
public void addCats(List<Pet> pets) {
pets.add(new Cat(...); // No compile error
}
Java Generics
That works !!!
Java Generics
And now that doesn't :-(
List<Pet> pets = new ArrayList<Pets>()
List<Dog> dogs = new ArrayList<Dogs>() // Dogs and dogs only !!
addCats(pets);
addCats(dogs); // Now this compiles
public void addCats(List<Pet> pets) {
pets.add(new Cat(...); // But now this doesn't compile :-(
}
Java Generics
Java Generics
Java Generics
<? super Xxx>
<? extends Xxx>
And as a general rule, don't return wildcards from a method...