1.BigInteger

BigInteger表示一个大整数

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

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

BigInteger构造方法

方法名

说明

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的对象

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

public class BigIntegerDemo1 {
    public static void main(String[] args) {
        BigInteger bd1 = new BigInteger(4,new Random());
        System.out.println(bd1);

        BigInteger bd2 = new BigInteger("123");
        System.out.println(bd2);

        BigInteger bd3 = new BigInteger("100",10);
        BigInteger bd4 = new BigInteger("100",2);
        System.out.println(bd3);//100
        System.out.println(bd4);//4

        //静态方法
        BigInteger bd5 = BigInteger.valueOf(100);
        System.out.println(bd5);
        //静态方法内部细节:
        //能表示的范围有限,只能在long的取值范围之内,如果超出long范围就不行了
        //在内部对常用数字-16~16进行了优化
        //提前把-16~16先创建好BigInteger对象,如果多次获取不会重新创建新的对象
        BigInteger bd6 = BigInteger.valueOf(16);
        BigInteger bd7 = BigInteger.valueOf(16);
        System.out.println(bd6 == bd7);//true

        BigInteger bd8 = BigInteger.valueOf(17);
        BigInteger bd9 = BigInteger.valueOf(17);
        System.out.println(bd8 == bd9);//false
    }
}

小结:

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

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( )

转为int类型整数,超出范围数据有误

import java.math.BigInteger;

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

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

        //减法
        BigInteger bd4 = bd1.subtract(bd2);
        System.out.println(bd4);//-10

        //乘法除法
        BigInteger bd5 = bd1.multiply(bd2);
        System.out.println(bd5);//200
        BigInteger bd6 = bd1.divide(bd2);
        System.out.println(bd6);//0
        BigInteger[] arr = bd1.divideAndRemainder(bd2);
        for (int i = 0; i < arr.length; i++) {
            BigInteger bd = arr[i];
            System.out.println(bd);//商:0  余数:10
        }
        System.out.println(arr.length);//2

        //equals 方法在本类已重写
        boolean result1 = bd2.equals(bd1);
        System.out.println(result1);//false
        boolean result2 = bd2.equals(bd7);
        System.out.println(result2);//true

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

        //max/min
        BigInteger max = bd1.max(bd2);
        System.out.println(max);//20
        System.out.println(max == bd1);//false
        System.out.println(max == bd2);//true
        BigInteger min = bd1.min(bd2);
        System.out.println(min);//10

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

2.BigDecimal

用于小数的精确计算

用来表示很大的小数

BigDecimal构造方法

方法名

说明

public  BigDecimal(double  val)

传递double类型小数来创建对象

public  BigDecimal(String  val)

传递字符串来创建对象

public  static  BigDecimal  valueOf(double  val)

静态方法获取BigDecimal对象

import java.math.BigDecimal;
import java.math.BigInteger;

public class BigDecimalDemo1 {
    public static void main(String[] args) {
        //通过传递double类型的小数来创建对象
        //细节:这种方式有可能是不精确的,所以不建议使用
        BigDecimal bd1 = new BigDecimal(0.01);
        BigDecimal bd2 = new BigDecimal(0.09);

        System.out.println(bd1);
        System.out.println(bd2);

        //通过传递字符串来创建对象
        BigDecimal bd3 = new BigDecimal("0.01");
        BigDecimal bd4 = new BigDecimal("0.09");
        BigDecimal bd5 = bd3.add(bd4);

        System.out.println(bd3);//0.01
        System.out.println(bd4);//0.09
        System.out.println(bd5);//0.10

        //通过静态方法获取对象
        BigDecimal bd6 = BigDecimal.valueOf(10);
        System.out.println(bd6);//10
    }
}

细节:

如果要表示的数字不大,没有超出double的取值范围,建议使用静态方法

如果要表示的数字比较大,超出了double的取值范围,建议使用构造方法

如果我们传递的是0~10之间的整数,包含0和10,那么方法会返回已经创建好的对象,不会重新new

BigDecimal的成员方法

方法名

说明

public  BigDecimal  add(BigDecimal  val)

加法

public  BigDecimal  subtract(BigDecimal  val)

减法

public  BigDecimal  multipiy(BigDecimal  val)

乘法

public  BigDecimal  divide(BigDecimal  val)

除法

public  BigDecimal  divide(BigDecimal  val,精确几位,舍入模式)

除法