为什么为 Java 8 选择双冒号运算符?
Why was the double colon operator chosen for Java 8?
在 Java 8 之前,我的观察是引用成员方法的实际标准是使用 #
符号(例如 Object#toString()
)。然后出现了 Java 8 ,它似乎没有理由选择 ::
运算符作为方法引用。
是否有关于为什么特别选择 ::
的官方解释或理由?
您可以找到 "official explanation or justification" from Brian Goetz。您应该阅读完整的讨论,但这是摘录:
The :: infix syntax:
ClassName::methodName
ClassName<T>::methodName
ClassName::<U>genericMethodName
works acceptably well. Some people like it, and some people hate it --
just like #. There's never going to be a perfect syntax for anything
that makes everyone jump up in unison and say "yeah, that's it!" But ::
is OK, and using up :: here is far better than using up #. (And, while
this might look a little weird to C++ programmers, the overlap between
the Java and C++ developer bases at this point is small enough that I
don't think we should be too worried about that.)
在 Java 8 之前,我的观察是引用成员方法的实际标准是使用 #
符号(例如 Object#toString()
)。然后出现了 Java 8 ,它似乎没有理由选择 ::
运算符作为方法引用。
是否有关于为什么特别选择 ::
的官方解释或理由?
您可以找到 "official explanation or justification" from Brian Goetz。您应该阅读完整的讨论,但这是摘录:
The :: infix syntax:
ClassName::methodName ClassName<T>::methodName ClassName::<U>genericMethodName
works acceptably well. Some people like it, and some people hate it -- just like #. There's never going to be a perfect syntax for anything that makes everyone jump up in unison and say "yeah, that's it!" But :: is OK, and using up :: here is far better than using up #. (And, while this might look a little weird to C++ programmers, the overlap between the Java and C++ developer bases at this point is small enough that I don't think we should be too worried about that.)