gpt4 book ai didi

java - 原始 int 也是 Comparable 吗?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:40:11 25 4
gpt4 key购买 nike

我刚看到一个 Java 泛型教程,它似乎使用原始 int、float 等作为扩展 Comparable 的对象

    public class MaximumTest {
// determines the largest of three Comparable objects

public static <T extends Comparable<T>> T maximum(T x, T y, T z) {
T max = x; // assume x is initially the largest

if(y.compareTo(max) > 0) {
max = y; // y is the largest so far
}

if(z.compareTo(max) > 0) {
max = z; // z is the largest now
}
return max; // returns the largest object
}

public static void main(String args[]) {
System.out.printf("Max of %d, %d and %d is %d\n\n",
3, 4, 5, maximum( 3, 4, 5 ));

System.out.printf("Max of %.1f,%.1f and %.1f is %.1f\n\n",
6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ));

System.out.printf("Max of %s, %s and %s is %s\n","pear",
"apple", "orange", maximum("pear", "apple", "orange"));
}
}

primitive 能否以某种方式成为 Comparable 对象?

最佳答案

原语将自动装箱为实现Comparable 的包装器对象。int val = 3; 将变成一个 Integer autoboxing and unboxing java

关于java - 原始 int 也是 Comparable 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41717476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com