简体   繁体   English

试图理解为什么两段相似的代码是不同的。 首先编译好。 二不

[英]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.

相关问题 为什么第一个编译但第二个不编译 - why the first compiles but second doesn't JVM内部 -​​ 为什么同一段代码在不同的时间运行? - JVM Internals - Why does the same piece of code runs in different timings? 为什么这段代码不起作用? - Why this piece of code does not work? 试图将两个数组组合成一个排序的数组; 第一个数组小于第二个数组时,代码不起作用 - Trying to combine two arrays into one sorted array; code does not work when first array is smaller than second array 为什么这两个相似的代码片会产生不同的结果? - Why do these two similar pieces of code produce different results? 当第一个if语句为真时,为什么代码执行第二行? - Why does the code execute the second line when the first if statement is true? 为什么这段代码写/打印所有 0.0 而不是不同的随机数? - Why does this piece of code write/print all 0.0 instead of different random numbers? 为什么这段带有两个for循环的代码没有O(N ^ 2)的Big O运行时? - Why does this piece of code with two for-loops not have Big O runtime of O(N^2)? 为什么这段代码会执行两条 println 语句,而不是一条一条打印? - Why does this piece of code execute two println statement instead of printing one by one? 两种相似的方法,不同的行为setSchema不会同时失败,但是第二种情况是MySQL异常吗? - Two similar methods, different behaviors setSchema does not fail in both, but MySQL exception in second case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM