java.math.BigInteger用来表示任意大小的整数。BigInteger内部用一个int[]数组来模拟一个非常大的整数;
java.math.BigInteger
BigInteger
int[]
和long型整数运算比,BigInteger不会有范围限制,但缺点是速度比较慢;
long
BigInteger和Integer、Long一样,也是不可变类,并且也继承自Number类;
Integer
Long
Number
使用longValueExact()方法时,如果超出了long型的范围,会抛出ArithmeticException;
longValueExact()
ArithmeticException
如果BigInteger的值超过了float的最大范围,返回 Infinity 。
float
static void testBigIntegerDemo() { BigInteger n = new BigInteger("999999").pow(99); floatValueDemo(n); longValueExactDemo(n); } static void floatValueDemo(BigInteger n) { System.out.println(n.floatValue()); // Infinity } static void longValueExactDemo(BigInteger n) { System.out.println(n.longValueExact()); // java.lang.ArithmeticException: BigInteger out of long range }
Sign in to make a reply
君似梦逍遥
java.math.BigInteger
用来表示任意大小的整数。BigInteger
内部用一个int[]
数组来模拟一个非常大的整数;和
long
型整数运算比,BigInteger
不会有范围限制,但缺点是速度比较慢;BigInteger
和Integer
、Long
一样,也是不可变类,并且也继承自Number
类;使用
longValueExact()
方法时,如果超出了long
型的范围,会抛出ArithmeticException
;如果
BigInteger
的值超过了float
的最大范围,返回 Infinity 。