Discuss / Java / 日常打卡

日常打卡

Topic source

java.math.BigInteger用来表示任意大小的整数。BigInteger内部用一个int[]数组来模拟一个非常大的整数;

long型整数运算比,BigInteger不会有范围限制,但缺点是速度比较慢;

BigIntegerIntegerLong一样,也是不可变类,并且也继承自Number类;

使用longValueExact()方法时,如果超出了long型的范围,会抛出ArithmeticException

如果BigInteger的值超过了float的最大范围,返回 Infinity 。


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
}

  • 1

Reply