Home >Java >javaTutorial >Improvements to generic target type inference methods in Java 8

Improvements to generic target type inference methods in Java 8

高洛峰
高洛峰Original
2017-01-18 11:12:001400browse

1. A simple understanding of generics

Generics are a new feature of Java SE 1.5. The essence of generics is a parameterized type, which means that the data type being operated is specified as a parameter. The popular point is "type variable". Variables of this type 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 convenient syntax that can save you some Java type conversion (casting) operations:

List<Apple> box = new ArrayList<Apple>();box.add(new Apple());Apple apple =box.get(0);

The above code itself has The expression is very clear: box is a List containing Apple objects. The get method returns an Apple object instance, and no type conversion is required in this process. Without generics, the above code needs to be written like this:

Apple apple = (Apple)box.get(0);

2. The embarrassment of generics

The biggest advantage of generics is that it provides program type safety and is backward compatible, but there are also The embarrassing part is that the type of the generic must be specified every time it is defined. Not only does it feel a bit verbose to explicitly specify it, the most important thing is that many programmers are not familiar with generics, so they often cannot give the correct type parameters. Now By automatically inferring the parameter types of generics through the compiler, such situations can be reduced and code readability improved.


3. Improvements in generic type inference in java7

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

Map<String, String> myMap = new HashMap<String, String>();

You may think: I have already specified the parameter type when declaring the variable, why do I need to specify it again when initializing the object? Fortunately, in Java SE 7, this method has been improved. Now you can use the following statement to declare and assign a value:

Map<String, String> myMap = new HashMap<>(); //注意后面的"<>"

In this statement, the compiler will automatically declare the variable based on the generic type when it is declared. The generic type is inferred when instantiating a HashMap. Again, be sure to pay attention to the "a8093152e673feb7aba1828c43532094" after new HashMap. Only adding this "a8093152e673feb7aba1828c43532094" means automatic type inference. Otherwise, it will be a non-generic type HashMap, and use the compiler to compile the source code. A warning will be given.

However: Java SE 7's type inference when creating generic instances is limited: only when the parameterized type of the constructor is explicitly declared in the context, type inference can be used, otherwise it will not work. For example: The following example cannot be compiled correctly in Java 7 (but it can be compiled in Java 8 now, because the type of generics is automatically inferred based on the method parameters):

List<String> list = new ArrayList<>();
list.add("A");// 由于addAll期望获得Collection<? extends String>类型的参数,因此下面的语句无法通过
list.addAll(new ArrayList<>());

4. Improvements in generic type inference in Java 8

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

1. Supports inferring generic target types through method context
2. Supports generic types in method call links Inference is passed to the last method
Let us look at the example on the official website:

class List<E> {
   static <Z> List<Z> nil() { ... };
   static <Z> List<Z> cons(Z head, List<Z> tail) { ... };
   E head() { ... }
}

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

//通过方法赋值的目标参数来自动推断泛型的类型
List<String> l = List.nil();
//而不是显示的指定类型
//List<String> l = List.<String>nil();
//通过前面方法参数类型推断泛型的类型
List.cons(42, List.nil());
//而不是显示的指定类型
//List.cons(42, List.<Integer>nil());

5. Summary

The above are the features of JEP101. As a representative of static languages, Java can be said to have a rich type system. The problem of converting between types troubles every Java programmer. Automatically inferring types through the compiler can slightly alleviate the problem of too complex type conversion. Although it is a small improvement, it will definitely have a huge impact on our programmers who write code every day. At least they will feel happier ~~ Maybe in Java 9, we will get a universal type var, like js or Like some dynamic languages ​​​​of scala^_^

For more articles related to the improvement of the generic target type inference method in Java8, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn