我希望两个put操作在下面的代码中抛出一个NullPointerException,但实际上lambda表达式工作正常,而方法引用只抛出一个NPE.
public static void main(String... args) {
Object object = null;
Map<String, FuncInterface> map = new HashMap<>();
map.put("key1", () -> object.notify()); // works
map.put("key2", object::notify); // throws NPE
}
@FunctionalInterface
private interface FuncInterface {
public void someAction();
}
Run Code Online (Sandbox Code Playgroud)
有什么不同?