在Java中,由CPU原生提供的整型最大范围是64位long
型整数。使用long
型整数可以直接通过CPU指令进行计算,速度非常快
java.math.BigInteger
就是用来表示任意大小的整数。BigInteger
内部用一个int[]
数组来模拟一个非常大的整数:
BigInteger bi = new BigInteger("1234567890"); System.out.println(bi.pow(5)); // 2867971860299718107233761438093672048294900000
BigInteger是不变类,并且继承自
Number
对BigInteger
做运算的时候,只能使用实例方法,
和long
型整数运算比,BigInteger
不会有范围限制,但缺点是速度比较慢
加法运算:
BigInteger i1 = new BigInteger("1234567890"); BigInteger i2 = new BigInteger("12345678901234567890"); BigInteger sum = i1.add(i2); // 12345678902469135780
乘法运算
BigInteger c = a.add(b); BigInteger d = c.multiply(b.add(BigInteger.valueOf(2)))
可以把BigInteger
转换成long
型:
BigInteger i = new BigInteger("123456789000"); System.out.println(i.longValue()); // 123456789000 System.out.println(i.multiply(i).longValueExact()); // java.lang.ArithmeticException: BigInteger out of long range
使用longValueExact()
方法时,如果超出了long
型的范围,会抛出ArithmeticException
BigInteger
和Integer
、Long
一样,也是不可变类,并且也继承自Number
类。因为Number
定义了转换为基本类型的几个方法:
byte
:byteValue()
short
:shortValue()
int
:intValue()
long
:longValue()
float
:floatValue()
double
:doubleValue()
因此,通过上述方法,可以把BigInteger
转换成基本类型。如果BigInteger
表示的范围超过了基本类型的范围,转换时将丢失高位信息,即结果不一定是准确的。如果需要准确地转换成基本类型,可以使
用intValueExact()
、longValueExact()
等方法,在转换时如果超出范围,将直接抛出ArithmeticException
异常
如果BigInteger
的值甚至超过了float
的最大范围(3.4x10^38)
import java.math.BigInteger; public class Main { public static void main(String[] args) { BigInteger n = new BigInteger("999999").pow(99); float f = n.floatValue(); System.out.println(f); } } >>> Infinity