[英]Trying to understand why two similar piece of code are different. First compiles well. Second does not
// compare 2 Strings // 比较 2 个字符串
BinaryOperator<String> maxLengthString = BinaryOperator.maxBy(
Comparator.comparingInt(String::length));
// compare 2 int // 比较 2 个整数
BinaryOperator<Integer> maxLengthString = BinaryOperator.maxBy(
Comparator.comparingInt(Integer::compareTo));
String::length takes 1 String and returns 1 int. String::length取 1 个 String 并返回 1 个 int。
Integer::compareTo takes 2 ints and returns 1 int. Integer::compareTo接受 2 个整数并返回 1 个整数。
Take a look at the methods' signature.查看方法的签名。
The compareTo method expects implicitly an argument to be passed. compareTo 方法需要隐式传递一个参数。 In this context the compiler can't retrieve the argument, so it throws a compile time error.
在这种情况下,编译器无法检索参数,因此会引发编译时错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.