Java-Enhanced Generics

Source: Internet
Author: User

Although Java 8 was released at the beginning of 2014, Java 9 waited until 2016, but some of the features currently planned for a future release (hopefully Java 10) have been merged.

In particular, there are two larger features that have begun prototyping, which are enhanced generics (enhanced generics) and value types (values Types). With enhanced generics, Java Developers can write code like list<int>, eliminating the pain of boxing basic types. Some of the new generic proposals are vague (or subtle) and need to be handled carefully, see Brian Goetz's explanation in a recent design article.

Java has been focusing on backwards compatibility, and this has been reconfirmed under Oracle's management. To do this, Oracle is looking for a strategy that is similar to generics introduced in Java 5, migrating incrementally.

The basic design problem that needs to be overcome is that the Java type system does not have a unified root. There is no such type in Java, which is both a subclass of object and a subclass of Int. As can be seen from the structure of the JVM bytecode, it is clear that returning an int from the method and returning an object with different opcode (opcode)--ireturn and Areturn are not the same.

The current prototype uses a type of variable called "any" to indicate that it can be either a reference type or a type variable of the base type (also including the newly proposed value type). The current writing Container<any T>, but the syntax will change before the feature is actually delivered.

The current idea is that,list<integer> and list<string> will continue to use the List.class representation at run time (so that type erasure will persist for reference types),list<int> will be represented with a different run-time type (possibly with a different class file). This approach is referred to as the basic type of "generics". This can also help another design problem by upgrading existing collection classes and using enhanced generics. Supporting developers using list<int> is a major design goal, from existing generic types to generic types that support any type of variable in future releases, and should be able to migrate past.

There are some strange places about how enhanced generics fit into the type system. For example,,list<int> is not a sub-type of List type (if so, this means that list<int> can store an instance of object). However, List<?> is a subtype of the List, so this means that list<int> is not a list<?> subtype, and wildcards cannot be applied to enhanced generics.

The current prototype is far from real product, there is a lot of design and implementation work to do. The implementation of the Special Initiative is actively underway. Automatic generation of special code is preferable (less manual boilerplate code), but this may require additional support in bytecode and class loading subsystems. Another interesting possibility is the introduction of a meta-programming facility at the virtual machine level (not directly supported at the Java language level). In invokedynamic analogy, this can be called "classdynamic", as described here.

The development of enhanced generics and value types is managed through Project Valhalla, and more details can be found in the project.

View English text: Under the Hood with A Prototype of enhanced generics for Java

Java-Enhanced Generics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.