英文:
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<String> maxLengthString = BinaryOperator.maxBy(
Comparator.comparingInt(String::length));
// compare 2 int
BinaryOperator<Integer> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论