共计 1504 个字符,预计需要花费 4 分钟才能阅读完成。
在 Java 中,由 CPU 原生提供的整型最大范围是 64 位 long
型整数。使用 long
型整数可以直接通过 CPU 指令进行计算,速度非常快。
如果我们使用的整数范围超过了 long
型怎么办?这个时候,就只能用软件来模拟一个大整数。java.math.BigInteger
就是用来表示任意大小的整数。BigInteger
内部用一个 int[]
数组来模拟一个非常大的整数:
BigInteger bi = new BigInteger("1234567890");
System.out.println(bi.pow(5)); // 2867971860299718107233761438093672048294900000
对 BigInteger
做运算的时候,只能使用实例方法,例如,加法运算:
BigInteger i1 = new BigInteger("1234567890");
BigInteger i2 = new BigInteger("12345678901234567890");
BigInteger sum = i1.add(i2); // 12345678902469135780
和 long
型整数运算比,BigInteger
不会有范围限制,但缺点是速度比较慢。
也可以把 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.4×1038),那么返回的 float 是什么呢?
// BigInteger to float
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
}
}
小结
BigInteger
用于表示任意大小的整数;
BigInteger
是不变类,并且继承自Number
;
将 BigInteger
转换成基本类型时可使用 longValueExact()
等方法保证结果准确。