【发布时间】:2020-02-21 08:54:45
【问题描述】:
1)
OptionalInt::of应该接受参数int,但是为什么下面的可以编译?
Integer boxed=2;
Optional<OptionalInt> optInt=Optional.ofNullable(boxed).map(OptionalInt::of);
Optional.ofNullable(boxed) 应该返回 Optional<Integer>,而不是 int。
2) 为什么下面的不能编译?
Optional.ofNullable(boxed).flatMap(OptionalInt::of);
【问题讨论】:
-
您可以在需要
int的地方传递Integer,这称为自动装箱。但是你不能在需要Optional的地方传递OptionalInt,这些不相关的类型之间没有转换。