如果要处理大数据可以使用BigInteger。
1.valueOf(parament); 将参数转换为制定的类型
比如 int a=1;
BigInteger b=BigInteger.valueOf(a);
则b=1;
String s=”12”;
BigInteger c=BigInteger.valueOf(s);
则c=12;
返回其值等于指定 long
的值的 BigInteger。提供的此“静态工厂方法”优先于 (long
) 构造方法,因为前者允许重用经常使用的 BigInteger。
2.add(); 大整数相加
BigInteger a=new BigInteger(“12”);
BigInteger b=new BigInteger(“34”);
System.out.println(a.add(b)); 46
返回其值为 (this + val) 的 BigInteger。
3.subtract(); 相减
返回其值为 (this - val) 的 BigInteger。
4.multiply(); 相乘
返回其值为 (this * val) 的 BigInteger。
5.divide(); 相除取整
返回其值为 (this / val) 的 BigInteger。
6.remainder(); 取余
返回其值为 (this % val) 的 BigInteger。
7.pow(); a.pow(b)=a^b
返回其值为 (thisexponent) 的 BigInteger。注意,exponent 是一个整数而不是 BigInteger。
8.gcd(); 最大公约数
返回一个 BigInteger,其值是 abs(this) 和 abs(val) 的最大公约数。如果 this==0 && val==0,则返回 0 。
9.abs(); 绝对值
返回其值是此 BigInteger 的绝对值的 BigInteger。
10.negate(); 取反数
返回此 BigInteger 的正负号函数。
11.mod(); a.mod(b)=a%b=a.remainder(b);
返回其值为 (this mod m) 的 BigInteger。此方法不同于 remainder,因为它始终返回一个 非负 BigInteger。
12.max(); 返回此 BigInteger 和 val 的最大值。
min();返回此 BigInteger 和 val 的最小值。
13.comareTo();
将此 BigInteger 与指定的 BigInteger 进行比较。对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=) 中的每一个运算符的各个方法,优先提供此方法。执行这些比较的建议语句是:(x.compareTo(y) <op> 0),其中 <op> 是六个比较运算符之一。
14.equals(); 是否相等
比较此 BigInteger 与指定的 Object 的相等性(boolean)
BigInteger num1 = new BigInteger("9999999999999999999999999999999999999999999999999999999");
BigInteger num2 = new BigInteger("9999999999999999999999999999999999999999999999999999999");
System.out.println("num1+num2=" + num1.add(num2));
System.out.println("num1-num2=" + num1.subtract(num2));
System.out.println("num1*num2=" + num1.multiply(num2));
System.out.println("num1/num2=" + num1.divide(num2));
System.out.println("num1%num2=" + num1.mod(num2));