This article is from my translation of the Infoq Chinese station, the original address is: Http://www.infoq.com/cn/news/2015/06/angular-2-react-native-roadmap
Richard and Raoul provide training courses on Java 8 to a broad range of developers. Recently, two people in Devoxx UK 2015 on a joint speech, reviewed the origin and motivation of Java generics, introduced some of the current unknown features, while the outlook for Java 10. The lecture consists of 3 wonderful parts: The past, the present and the future of generics.
Past
As Richard and Raoul say, before introducing generics, collections in Java are difficult to use because they contain object, and we cannot determine the concrete type of the elements in the collection. If an object of any type is added to the collection, its type information is lost, and when the object is fetched, the developer has to manually cast it to the relevant type to use it.
This not only causes the code to become unusually cumbersome, but also makes it impossible to limit the types of objects that are added to the collection. Java 5, released in 2004, solves this problem by adding generic features, but generics can only be implemented by type erasure for backward compatibility.
Right now
Next, Richard and Raoul talked about some of the features of the current Java generics, especially some unknown features: cross-type, wildcard, and F-bind polymorphism.
The cross type attribute is relatively simple to use, referring to the types of elements in the collection that need to inherit two or more classes or interfaces. Wildcard characters are not so well understood. Richard points out:
wildcard characters are difficult to understand and difficult to use. In the past, in Java, you cannot express the concept of covariance and contravariance of parameters, and for variables declared as an interface type, you need to represent their type information every time you declare a variable. This pushes complexity from the author of the Library to everyone.
Nonetheless, Richard and Raoul illustrate how programmers can benefit from the concept of associating different wildcard characters to producers and consumers.
Producers will produce objects for other classes to use. In order for these classes to know how to use these objects, we use a "contract" that represents the most specific type that can be provided. This "contract" takes the form of inheriting wildcards, specifically class< Extends T>. This indicates that the type used by the generic class is a subclass of T or T, which means that you can use all the behaviors that T has.
Similarly, the consumer receives and uses the object. In order for other classes to know what type of object can be passed to the consumer, the consumer needs to specify the most specific type that is guaranteed to be received. It takes the form of super wildcard, specifically class< Super t>. This symbol indicates that we intend to assign an object of type T to the consumer, so that the consumer can handle the object of the parent class of type T or T.
Finally, two speakers talked about F-binding polymorphism, which they called the "strange recursive Generic pattern" (analogous to the peculiar recursive template pattern in C + +). In this generic form, the type parameter recursively refers to the generic type itself, as follows:
Public interface Classname<t extends classname<t>>
As mentioned in the speech, this attribute is useful when a method in a generic type needs to return a template type, such as when cloning an object.
Future
In the final part of the speech, two speakers continue to introduce some of the improvements to generics that are currently under discussion, which may be added to Java 10. On the one hand, the native type of generics is currently being solved, so there is no need for automatic boxing and automatic unpacking; This is closely related to the project Valhalla. These changes, Richard says, allow programmers to write code like this:
list<int> integers = new arraylist<> (); Integers.add (1);
Richard said that the bottom can be implemented as int[] instead of the current integer[], so that on the one hand can reduce the memory footprint, on the other hand can let the CPU cache pre-gripper work better. He added: "This will improve performance, just like the generic generics of C #."
On the other hand, it is possible to simplify the process of covariance and inversion by letting the author of the library indicate changes in the interface declaration rather than now, when the interfaces are used. This form of declarative variables allows most developers to benefit from wildcard generics, eliminating the need for more information about the underlying, while advanced programmers can override this default behavior for more specialized code.
View English text: Richard Warburton and Raoul-gabriel Urma Review The history of generics in Java at Devoxx UK
Richard Warburton and Raoul-gabriel Urma review Java generics history on Devoxx UK 2015