常用API学习06(Java)

Biglnteger

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)     静态方法获取BigInteger的对象,内部有优化

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

针对BigInteger(String val, int radix)补充细节:字符串中的数字必须是整数;字符串中的数字必须要跟进制吻合;比如二进制中,那么只能写0和1,写其他的就报错。

针对static BigInteger valueOf(long val)补充细节:能表示的范围比较小,在long的取值范围之内,如果超出long的范围就不行了。在内部对常用的数字(-16~16)进行了优化。提前把-16~16先创建好BigInteger的对象,如果多次获取不会重新创建新的。

下面将代码实现:

package com.itheima.biginteger;

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

public class BigIntegerDemo01 {
    public static void main(String[] args) {
        //获取一个随机的大整数
        BigInteger bd1 = new BigInteger(4,new Random());//[0,15]
        System.out.println(bd1);

        //获取一个指定的大整数
        BigInteger bd2 = new BigInteger("99999999999999999999999999999999999999999");
        System.out.println(bd2);

        //获取一个指定进制的大整数
        BigInteger bd4 = new BigInteger("100",10);
        BigInteger bd5 = new BigInteger("11001011",2);
        System.out.println(bd4);
        System.out.println(bd5);

        //静态方法获取BigInteger对象,内部有优化
        BigInteger bd6 = BigInteger.valueOf(9999);
        System.out.println(bd6);

        BigInteger bd7 = BigInteger.valueOf(16);
        BigInteger bd8 = BigInteger.valueOf(16);
        System.out.println(bd7 == bd8);//地址相同

        BigInteger bd9 = BigInteger.valueOf(17);
        BigInteger bd10 = BigInteger.valueOf(17);
        System.out.println(bd9 == bd10);//地址不同

        //对象一旦创建,内部的数据不能发生改变
        BigInteger result = bd7.add(bd8);
        System.out.println(result);
        System.out.println(bd7==result);
        System.out.println(bd8==result);
    }
}

运行结果:

#总结:如果BigInteger表示的数字没有超出long的范围,可以用静态方法获取。如果BigInteger表示的超出long的范围,可以用构造方法获取。对象一旦创建,BigInteger内部记录的值不能发生改变。只要进行计算都会产生一个新的BigInteger对象。

Biglnteger常见成员方法

代码实现:

package com.itheima.biginteger;

import java.math.BigInteger;

public class BigIntegerDemo02 {
    public static void main(String[] args) {
        //创建两个BigInteger对象
        BigInteger bd1 = BigInteger.valueOf(10);
        BigInteger bd2 = BigInteger.valueOf(5);
        //加法
        System.out.println(bd1.add(bd2));
        //减法
        System.out.println(bd1.subtract(bd2));
        //乘法
        System.out.println(bd1.multiply(bd2));
        //除法,获取商
        System.out.println(bd1.divide(bd2));
        //比较是否相同
        System.out.println(bd1.equals(bd2));
        //次幂
        System.out.println(bd1.pow(2));
        //返回较大值
        System.out.println(bd1.max(bd2));
        //返回较小值
        System.out.println(bd1.min(bd2));
        //转为int类型整数,超出范围数据有误
        int i = bd1.intValue();
        System.out.println(i);
    }
}

接下来单独看一下:public BigInteger[] divideAndRemainder(BigInteger val)

package com.itheima.biginteger;

import java.math.BigInteger;

public class BigIntegerDemo03 {
    public static void main(String[] args) {
        BigInteger bd1 = BigInteger.valueOf(10);
        BigInteger bd2 = BigInteger.valueOf(5);
        //除法,获取商和余数
        BigInteger[] arr = bd1.divideAndRemainder(bd2);
        System.out.println(arr[0]);
        System.out.println(arr[1]);
    }

}

运行结果:

结果显示,数组的第一个数据是商,第二给数据是余。 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值