BigInteger

目录

BigInteger构造方法

BigInteger构造方法小结:

BigInteger常见成员方法:

BigInteger底层存储方式

总结


在Java中,整数有四种类型:byte,short,int,long;

在底层占用字节个数:byte1个字节,short2个字节,int4个字节,long8个字节;

BigInteger表示范围很广;

BigInteger构造方法

方法名说明
public BigInteger(int num,Random rnd)获取随机大整数,范围:[0~2的num次方-1]
public BigInteger(String val)获取指定的大整数
public BigInteger (String val,int radix)获取指定进制的大整数

细节:对象一旦创建,内部记录的值不能发生改变。

import java.math.BigInteger;
import java.util.Random;

public class BigIntegerDemo1 {
    public static void main(String[] args){
        //1.获取一个随机的大整数
        Random r=new Random();
        BigInteger bd1=new BigInteger(4,r);
        System.out.println(bd1);//0~15
        //2.获取一个指定的大整数
        BigInteger bd2=new BigInteger("100");
        System.out.println(bd2);//100
        //字符串中必须是整数,否则会报错;
        //3.获取指定进制的大整数
        BigInteger bd4=new BigInteger("100",2);
        System.out.println(bd4);//2进制的100:4
        //2进制只有0,1
        //细节:字符串中的数字必须是整数;
        //字符串中的数字必须要跟进制吻合;
        //比如二进制中,只能写0,1,否则会保持错;
        //4.静态方法获取BigInteger的对象,内部有优化;
        BigInteger bd5=BigInteger.valueOf(100);
        System.out.println(bd5);//100
        //1.能表示范围比较小,只能在long的取值范围之内,如果超出long范围就不行了;
        //2.在内部对常用的数字,-16~16进行了优化;
        //提前把-16~16先创建好BigInteger的对象,如果多次获取不会重新创建新的;
        BigInteger bd7=BigInteger.valueOf(16);
        BigInteger bd8=BigInteger.valueOf(16);
        System.out.println(bd7==bd8);//true

        //5.对象一旦创建,内部的数据不能发生改变;
        BigInteger bd9=BigInteger.valueOf(1);
        BigInteger bd10=BigInteger.valueOf(2);

        BigInteger result=bd9.add(bd10);
        System.out.println(result);//3
        //此时,不会修改参与计算的BigInteger对象中的值,而是产生了一个新的BigInteger对象记录3;



    }
}

BigInteger构造方法小结:

1.如果BigInteger表示的数字没有超出long的范围,可以用静态方法获取;

2.如果BigInteger表示的超出long的范围,可以用构造方法获取;

3.对象一旦创建,BigInteger内部记录的值不能发生改变;

4.只要进行计算都会产生一个新的BigInteger对象;

BigInteger常见成员方法:

方法名说明
public BigInteger add(BigInteger val)加法
public BigInteger subtract(BigInteger val)减法
public BigInteger multiply(BigInteger val)乘法
public BigInteger divide(BigInteger val)除法,获取商
public BigInteger[] divideRemainder(BigInteger val)

除法,获取商和余数

public boolean equals(Object x)比较是否相同
public BigInteger pow(int exponent)

次幂

public BigInteger max/min(BigInteger val)返回较大值、较小值
public int intValue(BigInteger val)转为Int 类型整数,超出范围数据有误

import java.math.BigInteger;

public class BigIntegerDemo2 {
    public static void main(String[] args) {
        //1.创建两个BigInteger对象
        BigInteger bd1=BigInteger.valueOf(10);
        BigInteger bd2=BigInteger.valueOf(5);

        //2.加法
        BigInteger bd3=bd1.add(bd2);
        System.out.println(bd3);//15

        //3.除法,获取商和余数;
        BigInteger[] arr = bd1.divideAndRemainder(bd2);
        System.out.println(arr[0]);//2
        System.out.println(arr[1]);//0
        //0索引是商,1索引是余数

        //4.比较是否相同
        boolean result=bd1.equals(bd2);
        System.out.println(result);

        //5.次幂
        BigInteger bd4 = bd1.pow(2);
        System.out.println(bd4);//100

        //6.max
        BigInteger bd5 = bd1.max(bd2);

        //7.转为int类型整数,超出范围数据有误;
        BigInteger bd6=BigInteger.valueOf(1000);
        int i=bd6.intValue();
        System.out.println(i);//1000




    }

}

BigInteger底层存储方式

1.对于计算机而言,其实是没有数据类型的概念的,都是0101010101.

2.数据类型是编程语言自己规定的;

BigInteger存储上限:

数组中最多存储元素个数:21亿多;

数组中的每一位能表示的数字:42亿多;

BigInteger能表示的最大数字为:42亿的21亿次方;

总结

1.BigInteger表示一个大整数;

2.获取BigInteger的对象:

BigInteger b1=BigInteger.valueOf(0.1);

BigInteger b1=new BigInteger("整数");

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值