Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to infer the target type of generics in Java 8

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

It is believed that many inexperienced people have no idea about how to infer the target type of generics in Java 8. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Simple understanding of generics

Generics are a new feature of Java SE 1.5. generics are parameterized types in nature, that is, the data type being operated on 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 think of it as a convenient syntax that saves you some operations on Java type conversion (casting):

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

The above code itself is clear: box is a List with an Apple object. The get method returns an instance of the Apple object, which does not require type conversion. There are no generics, and the above code needs to be written like this:

Apple apple = (Apple) box.get (0); embarrassment of generics

The advantage of generics is that it provides program type safety and backward compatibility, but there is also an awkward place, that is, the type of generics should be specified every time it is defined, which not only feels a bit lengthy to display. The most important thing is that many programmers are not familiar with generics, so they are often unable to give correct type parameters. Now the parameter types of generics can be automatically inferred by the compiler, which can reduce this situation. And improve the readability of the code.

Improvement of generic type inference in java7

Using generic types in previous versions required the addition of generic types on both sides when declaring and assigning values. For example:

Map myMap = new HashMap ()

You may think that Laozi already specified the parameter type when declaring the variable, so why does Mao have to specify it when initializing the object? Fortunately, this approach has been improved in Java SE 7, and you can now declare and assign values using the following statement:

Map myMap = new HashMap (); / / pay attention to the following ""

In this statement, the compiler automatically infers the generic type when instantiating HashMap based on the generic type when the variable is declared. Again, it is important to pay attention to the "" after new HashMap. Only adding this "" means automatic type inference, otherwise it is a HashMap of a non-generic type, and a warning is given when compiling the source code using the compiler.

However: Java SE 7's type inference when creating generic instances is limited: type inference can be used only if the parameterized type of the constructor is prominently declared in the context, otherwise not. For example, the following example does not compile correctly in java 7 (but now it can be compiled in java8 because the type of the generic type is automatically inferred from the method parameters):

List list = new ArrayList (); list.add ("A"); / / because addAll expects to get Collection

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report