尝试理解为什么两段相似的代码不同。第一段成功编译。第二段无法编译。

huangapple go评论118阅读模式
英文:

Trying to understand why two similar piece of code are different. First compiles well. Second does not

问题

// 比较两个字符串

BinaryOperator<String> maxLengthString = BinaryOperator.maxBy(
                Comparator.comparingInt(String::length));

---

// 比较两个整数

BinaryOperator<Integer> maxLengthString = BinaryOperator.maxBy(
                Comparator.comparingInt(Integer::compareTo));
英文:

// compare 2 Strings

BinaryOperator&lt;String&gt; maxLengthString = BinaryOperator.maxBy(
            Comparator.comparingInt(String::length));

// compare 2 int

BinaryOperator&lt;Integer&gt; maxLengthString = BinaryOperator.maxBy(
            Comparator.comparingInt(Integer::compareTo));

答案1

得分: 1

Sure, here's the translated content:

String::length 接受一个字符串(String)并返回一个整数(int)。

Integer::compareTo 接受两个整数(int)并返回一个整数(int)。

请查看方法的签名。

英文:

String::length takes 1 String and returns 1 int.

Integer::compareTo takes 2 ints and returns 1 int.

Take a look at the methods' signature.

答案2

得分: 0

compareTo方法隐式地期望传递一个参数。在这种情况下,编译器无法检索参数,因此会抛出编译时错误。

英文:

The compareTo method expects implicitly an argument to be passed. In this context the compiler can't retrieve the argument, so it throws a compile time error.

huangapple
  • 本文由 发表于 2020年8月24日 15:12:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63556276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定