Java – the difference between enumerations Expand zipentry > and enumeration?
Are there any differences between enumerations? Extending zipentry > and enumerating < zipentry >? If so, what's the difference?
Solution
When you have one of them, what can you do? Because the type parameter is only used for the "output" position, there is no actual difference On the other hand, there is a big difference in one of the aspects you can use
Suppose you have enumeration < jarentry > – you can't pass it to a person who takes enumeration < zientry > as one of its arguments You can pass it to a method that extends zipentry > by enumerating
This is more interesting when you use type parameters to use types at input and output locations – list < T > is the most obvious example The following are three examples of methods for parameter changes In each case, we will try to get one item from the list and add another
// Very strict - only a genuine List<T> will do public void Foo(List<T> list) { T element = list.get(0); // Valid list.add(element); // Valid } // Lax in one way: allows any List that's a List of a type // derived from T. public void Foo(List<? extends T> list) { T element = list.get(0); // Valid // Invalid - this Could be a list of a different type. // We don't want to add an Object to a List<String> list.add(element); } // Lax in the other way: allows any List that's a List of a type // upwards in T's inheritance hierarchy public void Foo(List<? super T> list) { // Invalid - we Could be asking a List<Object> for a String. T element = list.get(0); // Valid (assuming we get the element from somewhere) // the list must accept a new element of type T list.add(element); }
For more information, read:
>The Java language guide to Genetics > the Java genetics tutorial (PDF) > the Java genetics FAQ – especially the section on wildcards