API(Biginteger)
public class bigintegerDemon1 {
public static void main(String[] args) {
/*
public BigInteger(int num,Random rnd)//求指定的随机数的大整数,范围[0,2的num次方-1]
public BigInteger(String val)//求指定的字符串的大整数
public BigInteger(String val,int radix)//求指定的进制的字符串的大整数
public static BigInteger valueOf(long val)//求指定的长整数的大整数
public BigInteger add(BigInteger val)//求两个大整数的和
*/
//1.获取一个随机大整数
Random r=new Random();
BigInteger b1=new BigInteger(10,r);
System.out.println(b1);
//2.获取一个指定的字符串的大整数
BigInteger b2=new BigInteger("12687123647863484684768424");
System.out.println(b2);
//3.获取一个指定进制的字符串的大整数
BigInteger b3=new BigInteger("123456789",16);
System.out.println(b3);
//4.获取一个指定长整数的大整数
BigInteger b4=BigInteger.valueOf(123456789L);//取值范围是long类型的取值范围
System.out.println(b4);
//5.对象一旦创建,内部数据不能改变
BigInteger b5=BigInteger.valueOf(1);
BigInteger b6=BigInteger.valueOf(2);
BigInteger result=b5.add(b6);
System.out.println(result);//3
//此时产生一个新的对象存储结果,原来的对象没有改变
System.out.println(b5);//1
System.out.println(b6);//2
}
}
---------------------------------------
public class bigintegerDemon2 {
public static void main(String[] args) {
/*
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类型,不可超出范围
*/
//1.创建两个BigInteger对象
BigInteger b1=BigInteger.valueOf(10);
BigInteger b2=BigInteger.valueOf(5);
//2.求两个大整数的和
BigInteger result=b1.add(b2);
System.out.println(result);//15
//...
//3.求商和余数
BigInteger[] arr=b1.divideAndRemainder(b2);
System.out.println(arr[0]);//2:商
System.out.println(arr[1]);//0:余数
//4.次幂
BigInteger result2=b1.pow(3);//不能把bigInteger对象作为参数传递
System.out.println(result2);//1000
//5.返回较大值,较小值
BigInteger max=b1.max(b2);
BigInteger min=b1.min(b2);
System.out.println(max);//10
System.out.println(min);//5
//6.转为int,long,double...类型
int num=b1.intValue();
System.out.println(num);//10
long num2=b1.longValue();
System.out.println(num2);//10
double num3=b1.doubleValue();
System.out.println(num3);//10.0
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Hexo!