很多时候我们需要处理超出Integer长度的数,在C中有long long,而在JAVA中,我们可以借助BigInteger来解决此类问题,理论上BigInteger长度无限。




BigInteger  来自java.math.BigInteger包。




定义:


BigInteger  bi=new BigInteger("12345");
 
或BigInteger  bi=BigInteger.valueOf(12345);






常用方法:



1.add();  相加
 
 
 
2.subtract();  相减
3.multiply();  相乘
4.divide();     相除取整
5.remainder();  取余,同mod();
6.pow();   a.pow(b)=a^b b需为int类型
7.gcd();   最大公约数
8.abs(); 绝对值






import java.math.BigInteger;

public class Main {

	public static void main(String[] args) {

		BigInteger a = new BigInteger("34");
		BigInteger b = BigInteger.valueOf(4);

		System.out.println("加法: " + a.add(b));
		System.out.println("减法: " + a.subtract(b));
		System.out.println("乘法: " + a.multiply(b));
		System.out.println("除法: " + a.divide(b));
		System.out.println("取余: " + a.mod(b));
		System.out.println("3次方:" + a.pow(3));
	}
}



输出结果:




加法: 38
减法: 30
乘法: 136
除法: 8
取余: 2
4次方:1336336