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);
}
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);
}
Firstly , the below method doesn't compile as it is missing the return type and it should be
Long.MAX_VALUE
in place ofLong.Max_value
.The above method can never return
true
as you are comparing along
value withLong.MAX_VALUE
, see the method signature you can pass onlylong
there.Anylong
can be as big as theLong.MAX_VALUE
, it can't be bigger than that.You can try something like this with BigInteger class :
The below code will return
true
:You can't. If you have a method called
isBiggerThanMaxLong(long)
it should always returnfalse
.If you were to increment the bits of
Long.MAX_VALUE
, the next value should beLong.MIN_VALUE
. Read up on twos-complement and that should tell you why.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.That method can't return
true
. That's the point ofLong.MAX_VALUE
. It would be really confusing if its name were... false. Then it should be just calledLong.SOME_FAIRLY_LARGE_VALUE
and have literally zero reasonable uses. Just use Android'sisUserAGoat
, or you may roll your own function that always returnsfalse
.Note that a
long
in memory takes a fixed number of bytes. From Oracle: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.