Generics in Java


Upper Bounds: <? extends Foo>

  • Use bounded wildcard to increase API flexibility

The List<Number> is more restrictive than List<? extends Number> because the former matches a list of type Number only, whereas the latter matches a list of type Number or any of its subclasses

Unbounded Wildcards: <?>

  • void printList(List<?> list)
  • List<Object> and List<?> are not the same, we can only insert null into a List<?>.

Lower Bounded: <? super Foo>

  • restricts the unknown type to be a specific type or a super type of that type
  • void addNumbers(List<? super Integer> list) works on List<Integer>, List<Number>, and List<Object>
  • anything that can hold Integer values.

Unbounded Wildcards: <?>

  • when any object can be used when a method can be implemented using functionality provided in the Object class or When the code is independent of the type parameter

Wildcards and Subtyping

List<Integer> is not a subtype of List<Number>, these two types are not related. The common parent of List<Number> and List<Integer> is List<?>.

Guidelines for Wildcard Use

  • An “in” variable is defined with an upper bounded wildcard, using the extends keyword.
  • An “out” variable is defined with a lower bounded wildcard, using the super keyword.
  • In the case where the “in” variable can be accessed using methods defined in the Object class, use an unbounded wildcard.
  • In the case where the code needs to access the variable as both an “in” and an “out” variable, do not use a wildcard.
  • A list defined by List<? extends ...> can be informally thought of as read-only
  • we can only add null to List<? extends Integer>

Wildcard Examples

static <T extends Comparable<? super T>> void sort(List list)
  • T can implement Comparable<? super T>, not just Comparable
  • It means that a Student class can implement Comparable, where Student is a subclass of Person

Type Parameter Names

  • E, K, V, T, S, U, V, T1

Type Erasure

  • Replace generic type parameters with their bound if bounded type parameters are used.
  • Replace generic type parameters with Object if unbounded type parameters or <?> are used.
  • Insert type casts to preserve type safety.
  • Generate bridge methods to keep polymorphism in extended generic types.
  • A type parameter cannot be used to instantiate its object inside a method.
    • Class<T> clazz, T instance = clazz.newInstance();`
  • static type parameters are not allowed
  • no cast for generic type, except unbounded wildcards
  • instanceOf doesn’t work on generic type
  • No generic exception
  • Can’t capture generic type exception
  • Can’t have overloaded methods that can have the same signature after type erasure

Generic and Array doesn’t work well

  • Java Array are Covariant
    • Java array checks the type of values at runtime
    • Object objectArray[] = new Long[1]; works.
  • Generics are invariant
    • we cannot assign subclass type generic to its super class generic reference because in generics any two distinct types are neither a subtype nor a supertype.
    • List<Object> objectList = new ArrayList<Long>(); will not compile.
  • We can declare generic array, but we can’t create a generic array directly.

Don’t add any more type parameters than you have to

Generic in JDK Collection

  • A reifiable type is a type whose type information is fully available at runtime
  • Java collections have all been augmented with generics
  • For backwards compatibility some methods still take an Object type.

Subclassing with Generic Types

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)