Edited, memorised or added to reading list

on 08-Aug-2014 (Fri)

Do you want BuboFlash to help you learning these things? Click here to log in or create user.

Flashcard 149626698

Tags
#bloch-effective-java-2ed #java #java-generics
Question
A class or interface whose declaration has one or more [...]is a generic class or interface.
Answer
(formal) type parameters


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Open it
A class or interface whose declaration has one or more type parameters is a generic class or interface.







#java #java-generics
List<? extends Number> foo3

You can't add an Integer because foo3 could be pointing at a List<Double>.You can't add a Double because foo3 could be pointing at a List<Integer>.You can't add a Number because foo3 could be pointing at a List<Integer>.

statusnot read reprioritisations
last reprioritisation on reading queue position [%]
started reading on finished reading on




#java #java-generics

List<? super Integer> foo3

Reading - Given the above possible assignments, what type of object are you guaranteed to receive when you read from List foo3:

  • You aren't guaranteed an Integer because foo3 could be pointing at a List<Number> or List<Object>.
  • You aren't guaranteed an Number because foo3 could be pointing at a List<Object>.
  • The only guarantee is that you will get an instance of an Object or subclass of Object (but you don't know what subclass).

statusnot read reprioritisations
last reprioritisation on reading queue position [%]
started reading on finished reading on




#java #java-generics
"Producer Extends" - If you need a List to produce T values (you want to read Ts from the list), you need to declare it with ? extends T, e.g. List<? extends Integer>. But you cannot add to this list.

statusnot read reprioritisations
last reprioritisation on reading queue position [%]
started reading on finished reading on




#java #java-generics
"Consumer Super" - If you need a List to consume T values (you want to write Ts into the list), you need to declare it with ? super T, e.g. List<? super Integer>. But there are no guarantees what type of object you may read from this list.

statusnot read reprioritisations
last reprioritisation on reading queue position [%]
started reading on finished reading on




#java #java-generics
If you need to both read from and write to a list, you need to declare it exactly with no wildcards, e.g. List<Integer>.

statusnot read reprioritisations
last reprioritisation on reading queue position [%]
started reading on finished reading on




#java #java-generics

extends

The wildcard declaration of List<? extends Number> foo3 means that any of these are legal assignments:

List<? extends Number> foo3 = new ArrayList<Number>();  // Number "extends" Number (in this context)
List<? extends Number> foo3 = new ArrayList<Integer>(); // Integer extends Number
List<? extends Number> foo3 = new ArrayList<Double>();  // Double extends Number

statusnot read reprioritisations
last reprioritisation on reading queue position [%]
started reading on finished reading on




#java #java-generics

super

Now consider List < ? super T >.

The wildcard declaration of List<? super Integer> foo3 means that any of these are legal assignments:

List<? super Integer> foo3 = new ArrayList<Integer>();  // Integer is a "superclass" of Integer (in this context)
List<? super Integer> foo3 = new ArrayList<Number>();   // Number is a superclass of Integer
List<? super Integer> foo3 = new ArrayList<Object>();   // Object is a superclass of Integer

statusnot read reprioritisations
last reprioritisation on reading queue position [%]
started reading on finished reading on




#java #java-generics
You can't add any object to List<? extends T> because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object is allowed in that List. The only "guarantee" is that you can only read from it and you'll get a T or subclass of T.

statusnot read reprioritisations
last reprioritisation on reading queue position [%]
started reading on finished reading on