BigInteger()方法:
①获取一个随机的大整数:
public class Test3 {public static void main(String[] args) {Random r=new Random();BigInteger bigInteger=new BigInteger(4,r);System.out.println(bigInteger);}
}
②:获取一个指定的大整数:
public class Test3 {public static void main(String[] args) {BigInteger bigInteger=new BigInteger("9999999999999999999999999");System.out.println(bigInteger);}
}
③获取一个指定进制的大整数:
这表示这是一个二进制的100
public class Test3 {public static void main(String[] args) {BigInteger bigInteger=new BigInteger("100",2);System.out.println(bigInteger);}
}
先变成二进制的补码,再以32位为一组,再转换成十进制,正负号通过一个变量去表示,1表示正数,-1表示负数,放入数组当中。
BigDecima()方法:
用于小数的精确计算
用来表示很大的小数
1.通过传递double类型的小数来创建对象是不精确的,不建议使用。
public class Test3 {public static void main(String[] args) {BigDecimal bigDecimal=new BigDecimal(0.01);System.out.println(bigDecimal);}
}
2.通过传递字符串表示的小数来创建对象。
public class Test3 {public static void main(String[] args) {BigDecimal bigDecimal=new BigDecimal("0.01");System.out.println(bigDecimal);}}
3.通过静态方法获取对象。
public class Test3 {public static void main(String[] args) {BigDecimal bigDecimal=BigDecimal.valueOf(0.2);System.out.println(bigDecimal);}}
BigDecima()的底层存储方式: