有人告诉我,Java 8 方法引用并不像“真正的”函数指针那么强大。
怎么会这样呢?方法引用与函数指针有何不同?
最佳答案
What is currying?
柯里化(Currying)是从现有方法和参数构造新方法引用的能力。例如
// this is curried and implied in this method.
BiFunction<String, Integer> m = this::methodTwo;
// create a new curried method.
Function<String> m10 = t -> m.apply(t, 10);
一个真实的例子是消费者
// curry this method so that when called it prints to System.out
Consumer<String> out = System.out::println;
Java 8 method references aren't as powerful as 'true' function pointers.
Java 8 方法引用实际上是对封装虚拟方法的对象的引用。这在某种程度上非常有效,但有些基本的事情你可能认为你无法做到。例如equals()、toString()、表达式检查不会执行您可能期望的操作。
How are method references different from function pointers?
函数指针纯粹是对函数的引用。 Java 中的方法引用包括柯里化(Currying)信息,例如这是一个以 X、Y、Z 作为已知参数的方法。
does Java 8 support true closures?
Java 在功能方面趋于简约,但您可以认为 Java 中没有任何功能是“纯粹的”。它有太多的向后兼容性问题需要处理,即使有纯粹的意愿,也是不可能的。
do we still need to declare a variable final before being able to use it in a lambda?
不,“有效最终”就足够了。也就是说,如果您可以将其定为最终版本,那就可以了。
关于java - Java 8 方法引用与 'real' 函数指针有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25776637/