构造方法
public BigInteger(int num, Random rnd) //获取随机大整数,范围:[0 ~ 2的num次方-1]
public BigInteger(String val) //获取指定的大整数
public BigInteger(String val, int radix) //获取指定进制的大整数
下面这个不是构造,而是一个静态方法获取BigInteger对象
public static BigInteger valueOf(long val) //静态方法获取BigInteger的对象,内部有优化
构造方法小结:
-
如果BigInteger表示的数字没有超出long的范围,可以用静态方法获取。
-
如果BigInteger表示的超出long的范围,可以用构造方法获取。
-
对象一旦创建,BigInteger内部记录的值不能发生改变。
-
只要进行计算都会产生一个新的BigInteger对象
1.获取一个随机大整数
public class BigIntegerdemo1 {
public static void main(String[] args) {
//1.获取一个随机大整数
for (int i = 0; i < 100; i++) {
BigInteger bd1 = new BigInteger(4,new Random());
System.out.print(bd1+" ");//[0-15]
}
}
}
结果
2.获取指定的大整数可以超出long的取值范围
//细节:字符串中必须是整数,否则会报错
BigInteger bd2 = new BigInteger("1.1");
System.out.println(bd2);
结果
3.获取指定进制的大整数
//细节:
//1.字符串中的数字必须是整数
//2.字符串中的数字必须要跟进制吻合。
//比如二进制中,那么只能写日和1,写其他的就报错。
//3.获取指定进制的大整数
//细节:
//1.字符串中的数字必须是整数
//2.字符串中的数字必须要跟进制吻合。
//比如二进制中,那么只能写日和1,写其他的就报错。
BigInteger bd4 = new BigInteger("123", 2);
System.out.println(bd4)
4.静态方法获取BigInteger的对象,内部有优化
//细节:
//1.能表示范围比较小,只能在long的取值范围之内,如果超出long的范围就不行了。
//2.在内部对常用的数字: -16 ~ 16 进行了优化。
// 提前把-16~16 先创建好BigInteger的对象,如果多次获取不会重新创建新的。
BigInteger bd5 = BigInteger.valueOf(16);
BigInteger bd6 = BigInteger.valueOf(16);
System.out.println(bd5 == bd6);//true
BigInteger bd7 = BigInteger.valueOf(17);
BigInteger bd8 = BigInteger.valueOf(17);
System.out.println(bd7 == bd8);//false
5.对象一旦创建内部的数据不能发生改变
BigInteger bd9 =BigInteger.valueOf(1);
BigInteger bd10 =BigInteger.valueOf(2);
//此时,不会修改参与计算的BigInteger对象中的借,而是产生了一个新的BigInteger对象记录
BigInteger result=bd9.add(bd10);
System.out.println(result);//3
方法
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) 除法,获取商和余数
public boolean equals(Object x) 比较是否相同
public BigInteger pow(int exponent) 次幂
public BigInteger max/min(BigInteger val) 返回较大值/较小值
public int intValue(BigInteger val) 转为int类型整数,超出范围数据有误
//3.除法,获取商和余数
BigInteger[] arr = bd1.divideAndRemainder(bd2);
System.out.println(arr[0]);//2商
System.out.println(arr[1]);//0余
System.out.println("----------------");
创建两个BigInteger对象
//1.创建两个BigInteger对象
BigInteger bd1 = BigInteger.valueOf(10);
BigInteger bd2 = BigInteger.valueOf(5);
1.加法
//2.加法
BigInteger bd3 = bd1.add(bd2);
System.out.println(bd3);
结果
2.除法,获取商和余数
//3.除法,获取商和余数
BigInteger[] arr = bd1.divideAndRemainder(bd2);
System.out.println(arr[0]);//2商
System.out.println(arr[1]);//0余
System.out.println("----------------");
3.比较是否相同
boolean result = bd1.equals(bd2); System.out.println(result);//false System.out.println("----------------");
4.次幂
//5.次幂
BigInteger bd4 = bd1.pow(2);//100
System.out.println(bd4);
System.out.println("----------------");
5.求max值
BigInteger bd5 = bd1.max(bd2);//10
System.out.println(bd5);
System.out.println("----------------");
6.转为int类型整数,超出范围数据有误
/* BigInteger bd6 = BigInteger.valueOf(2147483647L);
int i = bd6.intValue();
System.out.println(i);
*/
BigInteger bd6 = BigInteger.valueOf(200);
double v = bd6.doubleValue();
System.out.println(v);//200.0
'