为什么我不能对 BigInteger 和 int 使用 equals?

Why i can't use equals for BigInteger and int?

当我尝试比较 BigIntegerint 时:

 BigInteger balance = new BigInteger(out_str.substring(186, 201).trim());
 if (!balance.equals(0)) {...}

我得到:

equals () between objects of inconvertible types 'int' and 'BigInteger'

使用

if (!balance.equals(BigInteger.ZERO)) {
    ...
}

intBigInteger 不共享任何 class 因为 int 是一个 原始类型 .

你只能比较至少有共同点的东西,比如Object。因此,像 BigInteger 与包装器 class Integer 的比较将编译,但结果将是 false,因为 Integer 不是 BigInteger。您需要将您的 int 转换BigInteger 以进行比较。

对于 0常数 BigInteger.ZERO (documentation):

if (!balance.equals(BigInteger.ZERO)) {
    ...
}