Re: Q:Generics
Kari Laine wrote:
I am learning generics and it is not easy...
These make it a little bit easier:
<http://download.oracle.com/javase/tutorial/extra/generics/index.html>
PDF free download about generics:
<http://java.sun.com/docs/books/effective/generics.pdf>
IBM DeveloperWorks - articles at all levels of knowledge:
<http://www.ibm.com/developerworks/search/searchResults.jsp?searchType=1&searchSite=dW&searchScope=javaZ&query=generics&Search=Search>
I can't figure out why,
public static<T> void copy(List<T> dst, List<? extends T> src)
The Book says that on this the type parameter can be only object - why?
What "Book" is that?
Type parameters can only ever be reference types. It is not accurate to say
that a type parameter can be an object. It is more correct to say that a type
parameter can be an extension of 'Object', the type. It is correct to say
that a type parameter represents a type that can be an extension of 'Object',
the type.
public static<T> void copy(List<? super T> dst, List<T> src)
The Book says that here the type parameter can be only integer - why?
Because the book is wrong, unless they're talking about 'Integer', the type.
There's more to the example than you're showing us, and that invisible part is
where the explanation lies.
It would help to know more, particularly of which "Book" you speak.
Hopefully someone can give some hand holding...
Give us something to work with and I'm sure someone will help.
Let me add my own thoughts in general, if I may.
Generics in Java are not "programming" in the procedural sense. They are a
bunch of declarations about the nature of types in the program. Generics,
with interfaces, support "type-based programming", a declarative style whereby
you lock down the type relationships.
'<? extends T>' is a declaration that the type in question is a subtype of
'T', the parametrized type.
'<? super T>' is a declaration type in question is a supertype of 'T', the
parametrized type.
These meanings of 'extends' and 'super' are completely distinct from those in
other contexts. Particularly, 'extends' in a type declaration represents
subtyping, which can be achieved either through a distinct meaning of
'extends' or of 'implements'.
In your first example, all the generic method knows about the base type of its
'List' argument is that it is a subtype of the determined type of 'T'. It
cannot safely say anything about which subtype it has found.
By comparison, the second example only knows that the base type of a 'List' is
some supertype of whatever it figures out 'T' to be. Again, it is unable to
determine specifically which supertype that is - it could be anything from 'T'
to 'Object' or some interface type.
The referenced reading material above explains how this affects the ability to
add items into or get items from the list.
'T' can only be a type that simultaneously satisfies all the declarations made
about it in a particular declaration. The compiler will reject any use of the
generic declaration that does not comply with all the generics restrictions in
that declaration.
A Java type declaration is a binding compile-time contract on the types in any
use-case expression of that declaration. Java uses generics and interfaces
synergistically to allow some pretty tight bindings. Ideally, the only code
to get through the compiler is code completely immune to 'ClassCastException'.
The matters of Java's compile-time and run-time enforcement policies are a
more advanced topic. Generics work at compile time only. There are
hand-coding tricks to employ when you need run-time type enforcement. The
mindsets differ for compile-time declaration and run-time enforcement.
--
Lew
The mindsets differ for Java and SQL programming in some of the same ways.