Ex02 Task2.1 Type mismatch error

Moderatoren: pmueller, SE - Design and Construction

Beiträge: 19
Registriert: 13. Apr 2016 16:29

Ex02 Task2.1 Type mismatch error

Beitrag von tud_std »


I have a question with regard to Context.get() method. I am wondering how does the Java compiler cast implicitly the return type of the Supplier call value.get() whose return type is of generic type T to Object type. In fact the signature of cache.computeIfAbsent() method is given such as : Object java.util.HashMap.computeIfAbsent(String key, Function<? super String, ? extends Object> mappingFunction)

Since T is an unbound generic type, the compiler should rather raise a type mismatch error which does not occur.


Beiträge: 448
Registriert: 25. Sep 2007 12:12

Re: Ex02 Task2.1 Type mismatch error

Beitrag von eichberg »

There is no cast necessary.All reference types are a subtype of Object and functions are generally co-variant in their return types and contra-variant in their parameter types.

BTW 1 - In Java Generics only abstract over reference types and Object is the top-most reference type.
BTW 2 -In Scala we have Any and below Any we have AnyRef (~Object) and AnyVal and all primitive types are subtypes of AnyVal; in Scala, a generic can abstract over any type...


Zurück zu „Software Engineering - Design and Construction“