Difference between revisions of "Java Generics"

From Wiki Notes @ WuJiewen.com, by Jiewen Wu
Jump to: navigation, search
m
m (Generics are not covariant)
Line 12: Line 12:
 
It turns out there's a good reason it doesn't work that way: It would break the type safety generics were supposed to provide. Imagine you could assign a List<Integer> to a List<Number>. Then the following code would allow you to put something that wasn't an Integer into a List<Integer>:
 
It turns out there's a good reason it doesn't work that way: It would break the type safety generics were supposed to provide. Imagine you could assign a List<Integer> to a List<Number>. Then the following code would allow you to put something that wasn't an Integer into a List<Integer>:
  
List<Integer> li = new ArrayList<Integer>();
+
List<Integer> li = new ArrayList<Integer>();
List<Number> ln = li; // illegal
+
List<Number> ln = li; // illegal
ln.add(new Float(3.1415));
+
ln.add(new Float(3.1415));
 
 
 
 
  
 
==References==
 
==References==
 
*[http://www.ibm.com/developerworks/java/library/j-jtp04298.html Going wild with generics, Part 1]
 
*[http://www.ibm.com/developerworks/java/library/j-jtp04298.html Going wild with generics, Part 1]
 
*[http://www.ibm.com/developerworks/java/library/j-jtp07018.html Going wild with generics, Part 2]
 
*[http://www.ibm.com/developerworks/java/library/j-jtp07018.html Going wild with generics, Part 2]

Revision as of 08:18, 14 March 2011

Generics are a means of expressing type constraints on the behavior of a class or method in terms of unknown types, such as "whatever the types of parameters x and y of this method are, they must be the same type," "you must provide a parameter of the same type to both of these methods," or "the return value of foo() is the same type as the parameter of bar()."

Wildcards

Wildcards — ? — are a means of expressing a type constraint in terms of an unknown type. They were not part of the original design for generics (derived from the Generic Java (GJ) project); they were added as the design process played out over the five years between the formation of JSR 14 and its final release.

The wildcard type List<?> is different from both the raw type List and the concrete type List<Object>. To say a variable x has type List<?> means that there exists some type T for which x is of type List<T>, that x is homogeneous even though we don't know what particular type its elements have. It's not that the contents can be anything, it's that we don't know what the type constraints on the contents are — but we know that there is a constraint. On the other hand, the raw type List is heterogeneous; we are not able to place any type constraints on its elements, and the concrete type List<Object> means that we explicitly know that it can contain any object.

Generics

Generics are not covariant

Arrays are covariant; because Integer is a subtype of Number, the array type Integer[] is a subtype of Number[], and therefore an Integer[] value can be supplied wherever a value of Number[] is required. (More formally, if Number is a supertype of Integer, then Number[] is a supertype of Integer[].) On the other hand, generics are not covariant; List<Integer> is not a subtype of List<Number>, and attempting to supply a List<Integer> where a List<Number> is demanded is a type error.

It turns out there's a good reason it doesn't work that way: It would break the type safety generics were supposed to provide. Imagine you could assign a List<Integer> to a List<Number>. Then the following code would allow you to put something that wasn't an Integer into a List<Integer>:

List<Integer> li = new ArrayList<Integer>();
List<Number> ln = li; // illegal
ln.add(new Float(3.1415));

References