简体   繁体   中英

Long vs BigInteger

I understand that both java.lang.Long and java.math.BigInteger can 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?

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.

SOLUTION:

Scala.Long can hold far bigger number than the Scala.BigInt in Scala.

Below is the definition for variables with their maximum capacity.

val bigIntVariable = BigInt(1999999999)
val longvariable:Long = 9223372036854775807l //last character is l for long

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM