Java, comparing BigInteger values

前端 未结 3 1230
囚心锁ツ
囚心锁ツ 2021-02-05 05:25
BigInteger bigInteger = ...;


if(bigInteger.longValue() > 0) {  //original code
    //bigger than 0
}

//should I change to this?
if(bigInteger.compareTo(BigInteger.         


        
3条回答
  •  后悔当初
    2021-02-05 05:42

    If you are using BigInteger, this assumes you need bigger numbers than long can handle. So don't use longValue(). Use compareTo. With your example it better be:

    if (bigInteger.compareTo(BigInteger.ZERO) > 0) {
    
    }
    

提交回复
热议问题