Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A long bigger than 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); } 
like image 373
gongshw Avatar asked May 14 '13 14:05

gongshw


People also ask

What is long MAX_VALUE in Java?

The maximum value of long is 9,223,372,036,854,775,807. The Long. MAX_VALUE is a constant from the java. lang package used to store the maximum possible value for any long variable in Java.

What is long Min_value?

static long MIN_VALUE − This is a constant holding the minimum value a long can have, -263. static int SIZE − This is the number of bits used to represent a long value in two's complement binary form. static Class<Long> TYPE − This is the class instance representing the primitive type long.


2 Answers

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.

like image 184
djechlin Avatar answered Sep 27 '22 20:09

djechlin


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.

like image 33
James Cronen Avatar answered Sep 27 '22 19:09

James Cronen