JAVA BigInter
使用long等基本数据类型,标识的数据范围是有限的,JAVA中使用BigInter模拟大整数运算
BigInter在数学就是表示一个比较大的整数,在JAVA内部用一个int[]数组来模拟,没什么特别;
使用上,和基本类型有一些要注意:
BigInteger i1 = new BigInteger("1234567890");
BigInteger i2 = new BigInteger("12345678901234567890");
BigInteger sum = i1.add(i2); // 12345678902469135780
和long型整数运算比,BigInteger不会有范围限制,超出float表示的范围时,输出 Infinity
四则运算需要使用实例化方法
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 方法可以在运算超出范围时抛出异常;此外还有intValueExact
转换为基本类型:
- 转换为
byte:byteValue() - 转换为
short:shortValue() - 转换为
int:intValue() - 转换为
long:longValue() - 转换为
float:floatValue() - 转换为
double:doubleValue()

浙公网安备 33010602011771号