Demo.java:
package cn.xxx.demo;
import java.math.BigInteger;
/*
* 超级大的整数运算
* 超过long取值范围整数,封装成BigInteger类型的对象
*/
public class Demo {
public static void main(String[] args) {
function_1();
}
/*
* BigInteger对象的四则运算不能直接用运算符运算,必须通过方法运算。
* 调用方法计算,计算结果也只能是BigInteger对象(大整数)
*/
public static void function_1(){
BigInteger b1 = new BigInteger("5665464516451051581613661405146");
BigInteger b2 = new BigInteger("965855861461465516451051581613661405146");
BigInteger bigAdd = b1.add(b2); // 加法
System.out.println(bigAdd);
BigInteger bigSub = b1.subtract(b2); // 减法 b1-b2
System.out.println(bigSub);
BigInteger bigMul = b1.multiply(b2); // 乘法
System.out.println(bigMul);
BigInteger bigDiv = b2.divide(b1); // 除法 b2/b1
System.out.println(bigDiv);
}
public static void function(){
// BigInteger类的构造方法,传递字符串,要求数字格式(没有空格等),没有长度限制
BigInteger b = new BigInteger("8465846668464684562385634168451684568645684564564");
System.out.println(b);
BigInteger b1 = new BigInteger("5861694569514568465846668464684562385634168451684568645684564564");
System.out.println(b1);
}
}