Java中当处理非常大的数时,为什么不能直接用BigIntegera=BigInteger.valueOf(88888888888888888888);
BigInteger 类型的数字范围较 Integer 类型的数字范围要大得多。BigInteger 支持任意精度的整数。
在 BigInteger 类中封装了多种操作,除了基本的加、减、乘、除操作之外,还提供了绝对值、相反数、最大公约数以及判断是否为质数等操作。
使用 BigInteger 类,可以实例化一个 BigInteger 对象,彬自动调用相应的构造函数。BigInteger 类具有很多构造函数,但最直接的一种是参数以字符串形式代表要处理的数字。
public BigInteger(String val);
val 是十进制数字符串
BigInteger twoInstance = new BigInteger("2");
下面列举了 BigInteger 类中常用的几种运算方法
public BigInteger add(BigInteger val);//做加法运算
public BigInteger subftract(BigInteger val);//做减法运算
public BigInteger multiply(BigInteger val);//做乘法运算
public BigInteger divide(BigInteger val);//做除法运算
public BigInteger remainder(BigInteger val);//做取余运算
public BigInteger[] divideAndReainder(BigInteger val);//用数组返回余数和商,结果数组中第一个值为商,第二个值为余数
public BigInteger pow(int exponent);//取参数的 exponent 次方操作
public BigInteger negate();//取相反数
public BigInteger shiftLeft(int n);//将数字左移 n 位,如果 n 为负数,做右移操作
public BigInteger shiftRight(int n);//将数字右移 n 位,如果 n 为负数,做左移操作
public BigInteger and(BigInteger val);//与操作
public BigInteger or(BigInteger val);//或操作
public int compareTo(BigInteger val);//数字比较操作
public boolean equals(Object x);//当参数x是BigInteger类型的珠子并且数值相等时,返回true
public BigInteger min(BigInteger val);//返回较小的数字
public BigInteger max(BigInteger val);//返回较大的数字
例子:实现大数的各种运算操作
import java.math.BigInteger;
public class BigIntegerDemo{
public static void main(String[] args){
BigInteger bigInstance = new BigInteger("4");、、实例化一个大数字
//取该大数字 加 2 的操作
System.out.println("加法操作:"+bigInstance.add(new BigInteger("2")));
//取该大数字 减 2 的操作
System.out.println("减法操作:"+bigInstance.subtract(new BigInteger("2")));
//取该大数字 乘 2 的操作
System.out.println("乘法操作:"+bigInstance.multiply(new BigInteger("2")));
//取该大数字 除 2 的操作
System.out.println("除法操作:"+bigInstance.divide(new BigInteger("2")));
//取该大数字 除以 3 的商
System.out.println("取商:"+bigInstance.divideAndReainder(new BigInteger("2"))[0]);
//取该大数字 除以 3 的余数
System.out.println("取商:"+bigInstance.divideAndReainder(new BigInteger("2"))[1]);
//取该大数字的 2 次方
System.out.println("做 2 次方的操作:"+bigInstance.pow(2));
//取该大数字的相反数
System.out.println("取相反数操作:"+bigInstance.negate());
}
}
BigInteger的效率很低,而平时大多数时候不需要那么高精度。所以只在需要的时候用。
应该是因为使用BigInteger.valueOf()这个静态方法,其参数为一个long类型,所以其值也被限定在long的取值范围,即-9223372036854775808到9223372036854775807,而88888888888888888888 这个数超出了long的取值范围,所以会报错;
如果要创建一个值为88888888888888888888的BigInteger类的对象,可以用这个值的字符串的形式作为参数来创建一个BigInteger即可。
测试代码如下:
参考链接:
import java.math.BigInteger;
public class BigIntegerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// https://www.jianshu.com/p/71be9dfdfe95
BigInteger bStr = new BigInteger("88888888888888888888"); // 以此值字符串的形式作为参数创建一个BigInteger对象
System.out.println(bStr); // 打印它的值
// https://www.zhihu.com/question/497409505/answer/2213873644
// https://www.yulucn.com/question/8448260126
// https://zhidao.baidu.com/question/569740985295161844.html
BigInteger bLong = BigInteger.valueOf(9223372036854775807L); // 此静态方法以long为参数,其值必须在long的取值范围内
System.out.println(bLong); // 打印其值
BigInteger b2 = bStr.add(bLong); // 相加
System.out.println(b2);
}
}