BigInteger类的概述:可以让超过Integer范围内的数据进行运算
构造方法:
public BigInteger(String val)
成员方法:
public BigInteger add(BigInteger val) 加
public BigInteger subtract(BigInteger val) 减
public BigInteger multiply(BigInteger val) 乘
public BigInteger divide(BigInteger val) 除
public BigInteger[] divideAndRemainder(BigInteger val): 返回的是商和余数的数组
代码片段一:
//计算Integer的最大值
int maxValue = Integer.MAX_VALUE;
System.out.println("maxValue="+maxValue);
控制台打印:
代码片段二:(超过范围)
public static void main( String[] args )
{
Integer integer = new Integer("2147483648");
System.out.println("integer="+integer);
}
控制台打印:
原因:超过了范围。
用法:
public static void main( String[] args )
{
System.out.println("---加法运算----");
BigInteger i1 = new BigInteger("10");
BigInteger i2 = new BigInteger("20");
System.out.println(i1.add(i2));
System.out.println("---减法运算----");
System.out.println(i1.subtract(i2));
System.out.println("---乘法运算----");
System.out.println(i1.multiply(i2));
System.out.println("---除法运算----");
System.out.println(i1.divide(i2));
BigInteger[] bigIntegers = i1.divideAndRemainder(i2);
for (BigInteger b: bigIntegers){
System.out.println("b="+b);
}
}
控制台打印结果: