The original intention of Java's definition of generics is to ensure that errors that occur during runtime can be checked early at compile time. With this understanding, let's look at extends T> and super T>. super T> is called downward modeling, which means that the generic elements in the Comparable interface are all T or the parent class of T. When passing in parameters, you can pass in T or the subclass of T. extends T> is called upward shaping. Taking ArrayList extends T> as an example, it means that all elements in this list collection are subclasses of T and can be automatically converted to T type when taken out.
It’s a bit hard to explain, but it’s actually not that complicated. You’ll understand it after a few knocks.
If you want to get data from a data type, use the ? extends wildcard; If you want to write the object into a data structure, use the ? super wildcard; If you want to both save and retrieve, then don’t use it Wildcard.
is fixed and represents T or T’s parent class.
The original intention of Java's definition of generics is to ensure that errors that occur during runtime can be checked early at compile time. With this understanding, let's look at extends T> and super T>.
super T> is called downward modeling, which means that the generic elements in the Comparable interface are all T or the parent class of T. When passing in parameters, you can pass in T or the subclass of T.
extends T> is called upward shaping. Taking ArrayList extends T> as an example, it means that all elements in this list collection are subclasses of T and can be automatically converted to T type when taken out.
It’s a bit hard to explain, but it’s actually not that complicated. You’ll understand it after a few knocks.
If you want to get data from a data type, use the ? extends wildcard;
If you want to write the object into a data structure, use the ? super wildcard;
If you want to both save and retrieve, then don’t use it Wildcard.