【发布时间】:2018-11-05 07:58:07
【问题描述】:
鉴于此:
class MyClass {
static class A {
public boolean property() {
return Math.random() < 0.5;
}
}
static List<A> filterLambda(List<A> list) {
return list.stream().filter(a -> a.property()).collect(Collectors.toList());
}
static List<A> filterMethodCall(List<A> list) {
return list.stream().filter(A::property).collect(Collectors.toList());
}
}
- 每种方法的编译器做什么有什么区别?
- 如果有的话,内存使用或运行时是否存在差异? (即使很小,问题也只是学术问题)
PD:我知道这个问题类似于this one,但我认为它没有得到正确解决。
【问题讨论】:
-
“编译器做什么”可能超出了这里的答案范围。该规范提供了一些理论背景信息,但不清楚您想要深入了解实际编译器实现。我只能说为这两种方法生成的字节码是相同的。