Improvement of generalized target type inference method in Java8 _java

Source: Internet
Author: User
Tags addall java se

A simple understanding of generics

Generics are a new feature of the Java SE 1.5, the essence of which is a parameterized type, which means that the data type being manipulated is specified as a parameter. The popular point is "variable of type". This type of variable can be used in the creation of classes, interfaces, and methods.

The easiest way to understand Java generics is to think of it as a handy syntax that saves you from some of the actions on Java type conversions (casting):

Copy Code code as follows:
list<apple> box = new arraylist<apple> (); Box.add (new Apple ()); Apple Apple =box.get (0);

The above code itself has made it clear that box is a list of Apple objects. The Get method returns an instance of an Apple object that does not require a type conversion. Without generics, the code above needs to be written like this:
Copy Code code as follows:
Apple Apple = (apple) box.get (0);

Ii. the embarrassment of generics

The greatest advantage of generics is that it provides type safety for the program and can be backwards compatible. But there are also embarrassing places, that is, each time you define a generic type, so that the display specified not only feel a bit verbose, most of the programmers are unfamiliar with generics, so many times can not give the correct type parameters, It is now possible to automatically infer the parameter types of generics through the compiler, reducing the situation and improving the readability of the code.


Generic type inference improvement of JAVA7

Using a generic type in a previous release requires a generic type to be added to both sides when declaring and assigning a value. For example:

Copy Code code as follows:
map<string, string> mymap = new hashmap<string, string> ();

You may think: Lao Tze has indicated the parameter type when declaring the variable, is the wool also to specify when the object initializes? Thankfully, this approach is improved in Java SE 7, and now you can declare and assign a value using the following statement:
Copy Code code as follows:
map<string, string> mymap = new hashmap<> (); Notice the "<>" behind it.

In this statement, the compiler automatically infers the generic type when instantiating hashmap based on the generic type at the time of the variable declaration. Again, be sure to pay attention to the "<>" behind new HashMap, and only this "<>" means automatic type inference, otherwise non-generic type HashMap and a warning prompt when compiling source code using the compiler.

However: the type inference of Java SE 7 when creating generic instances is limited: only the parameterized type of the constructor is declared prominently in the context, so it is not possible to use type inference. For example: The following example is not compiled correctly in Java 7 (but now it can be compiled in JAVA8 because the type of the generic is automatically inferred based on the method parameters):

Copy Code code as follows:

list<string> list = new arraylist<> ();
List.add ("A");//Because AddAll expect to get collection<? Extends string> type parameters, so the following statement cannot pass the
List.addall (New arraylist<> ());


Generic type inference improvement of JAVA8

The target type of the generic type in JAVA8 is inferred mainly 2:

1. Supports inference of generic target types through the method context
2. Support for the passing of generic type inference to the last method in the method call link
Let's take a look at the official website example:

Copy Code code as follows:

Class List<e> {
Static <Z> list<z> nil () {...};
Static <Z> list<z> cons (Z head, list<z> tail) {...};
E Head () {...}
}

According to JEP101 's characteristics, we can write this when we call the above method

Copy Code code as follows:

To automatically infer the type of a generic by means of a method-assigned target parameter
list<string> L = List.nil ();
Instead of the specified type that is displayed
list<string> L = List.<string>nil ();
To infer a generic type from the previous method parameter type
List.cons (List.nil ());
Instead of the specified type that is displayed
List.cons (List.<integer>nil ());

V. Summary

The above is the characteristic content of JEP101, Java as the representative of static language, can say the type system is quite rich. The problem that causes conversions between types is troubling for every Java programmer, and it is possible for a compiler to automatically infer a type to mitigate a problem that is too complex for type conversions. Although it is small progress, but for us to write code every day programmers, certainly can bring huge effect, at least the mood is more cheerful ~ ~ Maybe in Java 9, we will get a generic type of Var, like JS or some of Scala's dynamic language ^_^

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.