相关疑难解决方法(0)

什么是原始类型,为什么我们不应该使用它?

问题:

  • 什么是Java中的原始类型,为什么我经常听说不应该在新代码中使用它们?
  • 如果我们不能使用原始类型,它有什么替代方案,它是如何更好的?

java generics raw-types

617
推荐指数
13
解决办法
20万
查看次数

Java Integer compareTo() - 为什么使用比较与减法?

我发现方法的java.lang.Integer实现compareTo如下:

public int compareTo(Integer anotherInteger) {
    int thisVal = this.value;
    int anotherVal = anotherInteger.value;
    return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}
Run Code Online (Sandbox Code Playgroud)

问题是为什么使用比较而不是减法:

return thisVal - anotherVal;
Run Code Online (Sandbox Code Playgroud)

java optimization comparison integer overflow

78
推荐指数
3
解决办法
1万
查看次数