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

I want this method to return true:

boolean isBiggerThanMaxLong(long val) {return (val > Long.MAX_VALUE);}
7

Best Answer


That method can't return true. That's the point of Long.MAX_VALUE. It would be really confusing if its name were... false. Then it should be just called Long.SOME_FAIRLY_LARGE_VALUE and have literally zero reasonable uses. Just use Android's isUserAGoat, or you may roll your own function that always returns false.

Note that a long in memory takes a fixed number of bytes. From Oracle:

long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.

As you may know from basic computer science or discrete math, there are 2^64 possible values for a long, since it is 64 bits. And as you know from discrete math or number theory or common sense, if there's only finitely many possibilities, one of them has to be the largest. That would be Long.MAX_VALUE. So you are asking something similar to "is there an integer that's >0 and < 1?" Mathematically nonsensical.

If you actually need this for something for real then use BigInteger class.

In Java, the maximum long value can be obtained using the constant Long.MAX_VALUE. This constant represents the largest positive value that can be stored in a variable of type long. It is equal to 2^63 - 1, which is approximately 9.22 quintillion.

The maximum long value is often used when dealing with large numbers or when defining ranges for variables. It can also be useful in scenarios where you need to set an upper limit for a variable.

When assigning a value larger than the maximum long value to a variable of type long, it will result in an overflow. This means that the value will wrap around and become negative.

It is important to be aware of the maximum long value in Java to avoid unexpected behavior and ensure the correctness of your code.

You can't. If you have a method called isBiggerThanMaxLong(long) it should always return false.

If you were to increment the bits of Long.MAX_VALUE, the next value should be Long.MIN_VALUE. Read up on twos-complement and that should tell you why.

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

If triangle.lborderA is indeed a long then the test in the original code is trivially true, and there is no way to test it. It is also useless.

However, if triangle.lborderA is a double, the comparison is useful and can be tested. isBiggerThanMaxLong(1e300) does return true.

 public static boolean isBiggerThanMaxLong(double in){return in > Long.MAX_VALUE;}

Just remember BigInteger is a class and long is a primitive. If you want to compare large values than long_max_value, wrap your value in the BigInteger class and treat them as objects.

A "long" greater than the maximum value of long just doesn't exist , rather i suggest you use the biginteger class , and caste the maximum value of long to a biginteger.