Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long vs BigInteger

I understand that both java.lang.Long and java.math.BigIntegercan hold very large natural numbers.

I also know Long's max value, but what is the max value for BigInteger?

And aside from capacity, would BigInteger ever perform better when working with generally large integers that still fall in Long's range?

Question

Is the only consideration: is my value too large for Long?

like image 555
Fritz Duchardt Avatar asked Jul 31 '15 13:07

Fritz Duchardt


People also ask

Is BigInt same as long?

The equivalent of Java long in the context of MySQL variables is BigInt. In Java, the long datatype takes 8 bytes while BigInt also takes the same number of bytes.

Is BigInteger bigger than long?

3. BigInteger Larger Than Long. MAX_VALUE. As we already know, the long data type is a 64-bit two's complement integer.

What is the difference between long and BigInteger in Java?

BigInteger is capable of holding far bigger numbers than Long. BigInteger seems capable of holding (2 ^ 32) ^ Integer. MAX_VALUE, though that depends on the implementation (and, even if truly unbounded in the implementation, there will eventually be a physical resource limit) See explanation here.

What is bigger than BigInteger?

Is there a variable type bigger than BigInteger? No there isn't. Biginteger uses byte arrays behind the scenes and just allocates more space for it whenever it needs it. It's size limit is the available memory.


1 Answers

BigInteger is capable of holding far bigger numbers than Long. BigInteger seems capable of holding (2 ^ 32) ^ Integer.MAX_VALUE, though that depends on the implementation (and, even if truly unbounded in the implementation, there will eventually be a physical resource limit) See explanation here.

The range of Long is [-9,223,372,036,854,775,808, +9,223,372,036,854,775,807].

Long will perform better than BigInteger so it really depends on what the size of your values will be. If they would all fall under Long's max value, it makes no sense not to use Long. If any would be bigger than the Long max value, you pretty much have to use BigInteger.

like image 76
AHungerArtist Avatar answered Sep 22 '22 17:09

AHungerArtist