How to determine the generic type returned by Arrays.asList in java?

recently read the holding container of java programming ideas (fourth edition). The code for 11 chapters and 223 pages of the book is as follows

clipboard.png


clipboard.png

Why is this?

Mar.12,2021

starting with Java7, officials have begun to enhance Java's ability to derive generic types. The corresponding items are JDK7's JEP 101: Generalized Target-Type Inference .

I think there is an answer upstairs that your code cannot be compiled in the following version of Java8, which is wrong. You can also compile your code correctly by using Java7, because JDK7 not only deduces generic types by diamond, but also adds some functions of target type derivation. You can refer to Oracle's official document Type Inference , see the Target Types section, and I extracted part:

.

The Java compiler takes advantage of target typing to infer the type parameters of a generic method invocation. The target type of an expression is the data type that the Java compiler expects depending on where the expression appears. Consider the method Collections.emptyList, which is declared as follows:

static <T> List<T> emptyList();

Consider the following assignment statement:

List<String> listOne = Collections.emptyList();

This statement is expecting an instance of List < String > ; this data type is the target type. Because the method emptyList returns a value of type List < T > , the compiler infers that the type argument T must be the value String . This works in both Java SE 7 and 8 .

as you can see, for JDK7 and later versions of JDK, they at least have the ability to determine generic types based on the return value.

< hr >

go back to your code, for example, you write:

List<Snow> snow1 = Arrays.asList(
    new Crusty(), new Slush(), new Powder()
);

the compiler can infer that each parameter you pass in is a Snow based on your return value type ( List < Snow > ), so as to determine whether the parameter is correct (that is, to determine whether the type of each parameter is Snow or its subclass). The effect is the same for the following code:

List<Snow> powders = Arrays.asList(
    new Light(), new Heavy()
);

because Light and Heavy are both indirect subclasses of Snow .

< hr >

if you don't write the return value, write the code directly:

Arrays.asList(
    new Light(), new Heavy()
);

then use the function of IntelliJ IDEA to automatically generate the returned variables (I think IDE is also the function of the relevant API provided by the calling compiler, or at least meets the requirements of the compiler):
Introduce local variable

List
Result of introducing local variable

Powder T Powder


Java


Java8Java8Java8


java8

clipboard.png

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3e232-2b937.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3e232-2b937.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?