BigInteger构造方法:
1.BigInteger(int num,Random rnd):
获取随机大整数,范围是[0 , 2^num - 1]
代码演示:
public class Test {
public static void main(String[] args) {
Random r = new Random();
BigInteger bd1 = new BigInteger(4, r);//[0,15]
System.out.println(bd1);
}
}
2.BigInteger(String val):
字符串中必须为整数,否则会报错
代码演示:
public class Test2 {
public static void main(String[] args) {
BigInteger bd2 = new BigInteger("999999999999999");
System.out.println(bd2);
}
}
基本可以当作它能够存储无限大的整数
运行结果:
3.BigInteger(String val, int radix):
获取指定进制的大整数
注:字符串中的数字要符合进制的规范,如radix进制为2,字符串中不能出现除了0,1以外的数字。
代码演示:
public class Test3 {
public static void main(String[] args) {
//2进制
BigInteger bd3 = new BigInteger("100", 2);
System.out.println(bd3);
//8进制
BigInteger bd4 = new BigInteger("100", 8);
System.out.println(bd4);
//10进制
BigInteger bd5 = new BigInteger("100", 10);
System.out.println(bd5);
//16进制
BigInteger bd6 = new BigInteger("100", 16);
System.out.println(bd6);
}
}
运行结果:
4.静态方法BigInteger.valueOf(int num):
静态方法获取BigInteger的对象
注:1.它能表示的范围比较小,在long的取值范围内
2.它在内部对常用的数字-16 ~ 16进行了优化,将-16 ~ 16提前创建好了BigInteger的对象,如 果多次获取不会再创建新的
代码演示:
public class Test4 {
public static void main(String[] args) {
BigInteger bd7 = BigInteger.valueOf(16);
BigInteger bd8 = BigInteger.valueOf(16);
//输出bd7存储元素
System.out.println(bd7);
//输出bd7存储的地址值和db8是否一致
System.out.println(bd7 == bd8);
}
}
运行结果:
注:使用BigInteger对象一旦创建,内部数据就不能发生改变。只要进行计算,则会产生一个新的BigInteger对象。
BigInteger成员方法:
1.加减乘除
代码演示:
public class Test5 {
public static void main(String[] args) {
BigInteger bd1 = new BigInteger("2");
BigInteger bd2 = new BigInteger("3");
//加法
BigInteger result1 = bd1.add(bd2);
System.out.println("result1:" + result1);
//减法
BigInteger result2 = bd1.subtract(bd2);
System.out.println("result2:" + result2);
//乘法
BigInteger result3 = bd1.multiply(bd2);
System.out.println("result3:" + result3);
//除法(结果会自动向下取整)
BigInteger result4 = bd1.divide(bd2);
System.out.println("result4:" + result4);
}
}
运行结果:
2.获取商和余数
代码演示:
public class Test5 {
public static void main(String[] args) {
BigInteger bd1 = new BigInteger("2");
BigInteger bd2 = new BigInteger("3");
BigInteger[] arr = bd2.divideAndRemainder(bd1);
//商
System.out.println("商:" + arr[0]);
//除数
System.out.println("除数:" + arr[1]);
}
}
使用这个方法会创建一个BigInteger数组对象,数组的长度为2,0索引位置为商,1索引位置为余数。
运行结果:
3.比较是否相同:
代码演示:
public class Test7 {
public static void main(String[] args) {
BigInteger bd1 = new BigInteger("2");
BigInteger bd2 = new BigInteger("3");
BigInteger bd3 = new BigInteger("3");
//bd1和bd2比较
boolean result1 = bd1.equals(bd2);
System.out.println(result1);
//bd2和bd3比较
boolean result2 = bd2.equals(bd3);
System.out.println(result2);
}
}
它返回的是一个布尔类型的值
运行结果:
4.次幂:
代码演示:
public class Test8 {
public static void main(String[] args) {
BigInteger bd1 = new BigInteger("2");
BigInteger bd2 = new BigInteger("3");
//bd1中存储元素的3次方
BigInteger result1 = bd1.pow(3);
System.out.println(result1);
//bd2中存储元素的2次方
BigInteger result2 = bd2.pow(2);
System.out.println(result2);
}
}
对象名.pow(int exponent),括号中只能填写int类型。
运行结果:
5.返回较大值/较小值
代码演示:
public class Test9 {
public static void main(String[] args) {
BigInteger bd1 = new BigInteger("2");
BigInteger bd2 = new BigInteger("3");
//较大值
BigInteger max = bd1.max(bd2);
System.out.println("较大值:" + max);
//较小值
BigInteger min = bd1.min(bd2);
System.out.println("较小值:" + min);
System.out.println(max == bd2);
System.out.println(min == bd1);
}
}
使用这个方法创建的变量引用了比较中的原变量,所以运行结果中显示它们存储的地址值是一样的,即使用这种方法没有再创建对象。
运行结果:
6.转为基本数据类型(超出范围数据有误):
代码演示:
public class Test10 {
public static void main(String[] args) {
BigInteger bd1 = new BigInteger("2");
BigInteger bd2 = new BigInteger("3");
//转换成int类型
int resultInt = bd1.intValue();
System.out.println(resultInt);
//转换成double类型
double resultDouble = bd1.doubleValue();
System.out.println(resultDouble);
//转换成long类型
long resultLong = bd1.longValue();
System.out.println(resultLong);
/*......等等等等.......*/
}
}
除去演示的这几个转换方法,还可以转换成其他类型,如下所示: