6.javaSE --- 常用API --- BigInteger(观b站黑马程序员,笔记)

BigInteger --- 不可变的任意精度的整数(对象一旦创建,内部记录的值是不能改变的)

继承结构:

        public class BigInteger extends Number implements Comparable<BigInteger>

字段摘要:

                1.ONE --- 常量1

                2.TEN --- 常量10

                3.ZERO --- 常量0

如何获取BigInteger对象:

        1.BigInteger(int num , Random rnd) --- 获取随机大整数,范围[0~2的num次方-1]

package com.test.BigIntegertest;

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

public class BigInteger1 {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            BigInteger bi = new BigInteger(4,new Random());
            System.out.print(bi+",");
        }
    }
}

       

 2^4 = 16,所以取值范围在[0~15]之间

        2.BigInteger(String val) --- 获取指定的大整数(其实就是把一个字符串整数转换为数据类型的整数)

package com.test.BigIntegertest;

import java.math.BigInteger;

public class BigInteger1 {
    public static void main(String[] args) {
        BigInteger bi = new BigInteger("12345");
        System.out.println(bi);
    }
}

       

         3.BigInteger(String val, int radix) --- 获取指定进制的大整数

package com.test.BigIntegertest;

import java.math.BigInteger;

public class BigInteger1 {
    public static void main(String[] args) {
        BigInteger bi = new BigInteger("101",2);
        System.out.println(bi);
    }
}

 可以看见,我让字符串的整数为101,后面进制权位为2,那么这个101就是2禁止的101,通过System.out.printf()转换以十进制打印出来

        4.public static BigInteger valueOf(long val) --- 静态方法获取BigInteger的对象,内部有优化

package com.test.BigIntegertest;

import java.math.BigInteger;

public class BigInteger1 {
    public static void main(String[] args) {
        BigInteger b0 = BigInteger.valueOf(100);
        System.out.println(b0);

        //因为这里的参数为long类型的,所以获取long类型的最大值
        System.out.println(Long.MAX_VALUE);
        //这一部分表示的内部优化
        BigInteger b1 = BigInteger.valueOf(16);
        BigInteger b2 = BigInteger.valueOf(16);
        System.out.println(b1 == b2);
    }
}

        为什么有BigInteger(String val),还要有BigInteger valueOf()方法呢,有以下两点:

                1.BigInteger valueOf()比BigInteger(String val)小,只能在long类型的取值范围之内,如果超出long类型的最大值9223372036854775807(不用记),则会报错

                2.在内部对常用的数字-16~16进行优化:在方法本身代码当中,它已经提前把-16~16创建好了BigInteger的对象,如果多次获取,那么不会返回你自己重新创建的对象,而是返回已有的对象,所以 == 表示地址值比较时,即使你自己创建了对象,但是还是给你返回true。

常用方法:

        1.public BigInteger add(BigInteger val) --- 加法

        2.public BigInteger subtract (BigInteger val) --- 减法

        3.public BigInteger multiply(BigInteger val) --- 乘法

        4.public BigInteger divide(BigInteger val) --- 除法,获取商

        5.public BigInteger[] diviAndRemainder(BigInteger val ) --- 除法,获取商和余数

        6.public boolean equals(Object x) --- 比较是否相同(BigInteger有比较内容的equals重写方法)

        7.public BigInteger pow(int exponent) --- 次幂

        8.public BigInteger max/min(BigInteger val) --- 返回较大值/较小值

        9.public int intValue(BigInteger val) --- 转为int类型整数,超出范围数据有误

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值