长春市网站开发,南庄建网站服务,海南城乡与建设厅网站,有的网站为什么打不开怎么回事BigInteger
在Java中#xff0c;整数类型有四种类型#xff1a;byte#xff08;一个字节#xff09;、short#xff08;2个字节#xff09;、int#xff08;四个字节#xff09;、long#xff08;8个字节#xff09;
超出取值范围上面类型无法使用#xff0c;这时…BigInteger
在Java中整数类型有四种类型byte一个字节、short2个字节、int四个字节、long8个字节
超出取值范围上面类型无法使用这时就需要用BigInteger他有范围但十分巨大
常用方法
1、public BigInteger(int num,Random rnd) //获取随机大整数范围[0~2的num次方-1]
2、public BigInteger(String val) //获取指定的大整数字符串必须是整数
3、public BigInteger(String val,int radix) //获取指定进制的大整数字符串必须是整数
4、public static BigInteger valueOf(long val) //静态方法获取BigInteger的对象内部有优化
4与2相比4只能在long数据范围内
4在内部对常用的数字-16~16进行了优化如果多次获取不会重新创建新的
4对象创建了内部数据不会改变
import java.math.BigInteger;
import java.util.Random;public class Main {public static void main(String[] args) throws CloneNotSupportedException{//1、public BigInteger(int num,Random rnd) //获取随机大整数范围[0~2的num次方-1]for(int i0;i10;i){BigInteger b1new BigInteger(3,new Random());System.out.print(b1 );}// 2、public BigInteger(String val)//获取指定的大整数字符串必须是整数BigInteger b2new BigInteger(99999999);System.out.println(b2);//3、public BigInteger(String val,int radix)//获取指定进制的大整数字符串必须是整数BigInteger b3new BigInteger(99999,16);System.out.println(b3);//4、public static BigInteger valueOf(long val)//静态方法获取BigInteger的对象内部有优化// 4与2相比4只能在long数据范围内//4在内部对常用的数字-16~16进行了优化如果多次获取不会重新创建新的BigInteger b6BigInteger.valueOf(11);BigInteger b7BigInteger.valueOf(11);System.out.println(b6b7);//trueBigInteger b4BigInteger.valueOf(99999);BigInteger b5BigInteger.valueOf(99999);System.out.println(b5b4);//false//4对象创建了内部数据不会改变BigInteger resultb6.add(b7);System.out.println(b6result);//falseSystem.out.println(b7result);//false}
} BigInteger常见成员方法
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类型整数超出范围数据有误 都用对象.方法名(参数)即可divideAndRemainder需要用数组接收