Target type inference of generics in Java 8_ Power node Java college sorting

Simple understanding of generics

Generics is a new feature of Java se 1.5. The essence of generics is parameterized types, that is, the data type operated is specified as a parameter. The popular point will be "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 regard it as a convenient syntax, which can save you some operations on Java type casting:

The above code itself has been clearly expressed: @ r_ 975_ 2419 @ is a list containing apple objects. The get method returns an apple object instance. This process does not require type conversion. Without generics, the above code needs to be written as follows:

Generic embarrassment

The biggest advantage of generics is that it provides program type safety and backward compatibility. However, it is also embarrassing that the type of generics should be specified every time. In this way, the display and designation not only feels a little lengthy, but also many programmers are not familiar with generics, so they can't give correct type parameters many times, Now the compiler can automatically infer the parameter types of generic types, which can reduce this situation and improve the readability of the code.

Improvement of generic type inference in Java 7

In previous versions, when using generic types, you need to add generic types on both sides when declaring and assigning values. For example:

You may think: Lao Tzu has specified the parameter type when declaring the variable. If it is gross, do you have to specify it when initializing the object? Fortunately, this method has been improved in Java se 7. Now you can use the following statement to declare and assign values:

In this statement, the compiler will automatically infer the generic type when instantiating HashMap according to the generic type at the time of variable declaration. Again, you must pay attention to the "< >" after the new HashMap. Only adding this "< >" means automatic type inference, otherwise it is a non generic HashMap, and a warning prompt will be given when compiling the source code with the compiler.

However, the type inference of Java se 7 when creating generic instances is limited: type inference can only be used if the parameterized type of the constructor is significantly declared in the context, otherwise it cannot be used. For example, the following example cannot be compiled correctly in Java 7 (but now it can be compiled in Java 8, because the type of generic type is automatically inferred according to the method parameters):

Improvement of generic type inference in Java 8

There are two main types of generic target type inference in java8:

1. Support inferring generic target type through method context

2. Support the transfer of generic type inference to the last method in the method call link

Let's take an example of the official website

According to the characteristics of jep101, we can write this when calling the above method

summary

These are the features of jep101. As a representative of static language, Java has a rich type system. The problem of mutual conversion between types puzzles every java programmer. Automatically inferring types through the compiler can slightly alleviate the problem of too complex type conversion. Although it is a small progress, it will certainly bring great effect to our programmers who write code every day, at least in a happier mood ~ ~ maybe in Java 9, we will get a general type VaR, like some dynamic languages of JS or Scala^_^

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>