The following Java code does not compile (using javac 1.8.0_121)
import java.util.Optional;
class B<T> {}
public class Test {
static B<Integer> f1(B<Object> a) { return null; }
static B<Integer> f2() {
Optional<B> opt = Optional.empty();
return opt.map(Test::f1).get();
}
}
My question is: why the code does not compile as above , and why it compiles if I change f1to take a raw type:
static B<Integer> f1(B a) { return null; }
, opt.map Optional<Object> ( Optional<B<Integer>>), ? (JLS 4.8), , (, ). opt , . , ( a raw B B<Object>) ?
Error java: incompatible types: java.lang.Object cannot be converted to B<java.lang.Integer>