I have code that could not be compiled using JDK 7, but it can be compiled using JDK 8.
For abstract actual code:
interface A { ... } class B implements A { ... } public void AAA(List<A> list) {...} AAA(Collections.singletonList(new B()));
Collections.singletonList is defined as
public static <T> List<T> singletonList(T o) { return new SingletonList<>(o); }
So, as far as I know, based on generic, T will be inferred to B, so Collections.singletonList (new B ()) will be a List, which cannot be assigned to a List, since Java generic is invariant.
But with JDK 8 T is output to A and compilation succeeds.
I would like to know how T is displayed on A, since there are two variables for type T: A and B.
Is there a priority order? Or does the compiler find a common class of ancestors?
Attaching an official document is much appreciated!
Thanks in advance!
ps1. JDK version 7 is Oracle 1.7.0_79, and JDK version 8 is Oracle 1.8.0_66.
ps2. :
https://github.com/apache/storm/blob/85a31e2fdec1ffef83e1ff438cd765a821fb06e4/examples/storm-opentsdb-examples/src/main/java/org/apache/storm/opentsdb/SampleOpenTsdbBoltTopology.java#L48
https://github.com/apache/storm/blob/85a31e2fdec1ffef83e1ff438cd765a821fb06e4/external/storm-opentsdb/src/main/java/org/apache/storm/opentsdb/bolt/OpenTsdbBolt.java#L77
https://github.com/apache/storm/blob/85a31e2fdec1ffef83e1ff438cd765a821fb06e4/external/storm-opentsdb/src/main/java/org/apache/storm/opentsdb/bolt/TupleOpenTsdbDatapointMapper.java#L37
, , §18. Type Inference, , . , , :
Java SE 7 Edition Java® :- ., . ., .( ) ( )., , , , .( ) .. . , , .: .
Java SE 7 Edition Java® :
. , , , AAA .
AAA
, , AAA . List<A>. " ", , , (B) (A).
List<A>
B
A
.
, ...
,
AAA(Collections.singletonList(new B())); // returns List<A> NOT List<B>
jdk-7 , T B, .
" " http://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html
Java SE 8. , , , , processStringList.
AAA A, , , AAA(), A, Java8 A over B ?
Source: https://habr.com/ru/post/1014226/More articles:Unallocated local variable and short circuit rating - c #Добавить навигационный ящик в существующую деятельность - java"Using an unassigned local variable" in an if statement using TryParse using dynamic - c #How to determine how many capture groups are in Perl Regexp? - regexПравильно ожидая в F # метод async С# с возвращаемым типом Task - c#https://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1014227/why-is-evaluating-keys-to-whnfs-enough-to-construct-sets-in-haskell&usg=ALkJrhgjsQjLMcPn1LroVX5yUFxIvv5kzwComparator for Java classes? - javaWhy is my Service Fabric code blocking my own PDBs? - c #Variable type escapes scope when combining a generic and not a generic class - genericsDetect change in child component variable caused by parent angular 2 - angularAll Articles