A long bigger than Long.MAX_VALUE

前端 未结 4 1497
天涯浪人
天涯浪人 2020-12-10 00:37

How can I get a long number bigger than Long.MAX_VALUE?

I want this method to return true:

boolean isBiggerThanMaxLong(long val) {
    r         


        
4条回答
  •  再見小時候
    2020-12-10 01:06

    Firstly, the below method doesn't compile as it is missing the return type and it should be Long.MAX_VALUE in place of Long.Max_value.

    public static boolean isBiggerThanMaxLong(long value) {
          return value > Long.Max_value;
    }
    

    The above method can never return true as you are comparing a long value with Long.MAX_VALUE , see the method signature you can pass only long there.Any long can be as big as the Long.MAX_VALUE, it can't be bigger than that.

    You can try something like this with BigInteger class :

    public static boolean isBiggerThanMaxLong(BigInteger l){
        return l.compareTo(BigInteger.valueOf(Long.MAX_VALUE))==1?true:false;
    }
    

    The below code will return true :

    BigInteger big3 = BigInteger.valueOf(Long.MAX_VALUE).
                      add(BigInteger.valueOf(Long.MAX_VALUE));
    System.out.println(isBiggerThanMaxLong(big3)); // prints true
    

提交回复
热议问题