目录
在Java编程中,处理整数时常常会遇到精度问题。尤其在进行大数计算时,常规的int和long类型无法满足需求。为了解决这个问题,java提供了BigInteger类,这是个可以处理任意精度整数的类。
一、什么是BigInteger
1、BigInteger的定义
BigInteger类位于 java.math 包中,是Java提供的一个用于进行任意精度整数计算的类。它可以表示比 long 更大的整数,且不受限制于固定的位数。Biglnteger 采用了不可变的设计,即一旦创建,就无法改变其值。
2、BigInteger的基本特性
1、任意精度:BigInteger 可以表示任意大小的整数,仅受限于计算机的内存。
2、不可变性:BigInteger 对象是不可变的,一旦创建,其值不能被改变。
3、丰富的API:BigInteger 提供了大量方法用于整数的运算、比较和转换。
4.、支持符号:BigInteger 可以表示正整数、负整数和零。
二、创建和使用BigInteger
BigInteger b1 = new BigInteger("987654321123456789098765432178761223323123");
System.out.println("BigInteger:" + b1);
BigInteger b2 = BigInteger.valueOf(12345);
BigInteger b3 = BigInteger.valueOf(12345678909876L);
1、四则运算
Biglnteger类提供了多种方法用于进行基本的数学运算,包括加法,减法,乘法,除法。
BigInteger add = b1.add(b2);
BigInteger subtract = b1.subtract(b2);
BigInteger multiply = b1.multiply(b2);
BigInteger divide = b1.divide(b2);
System.out.println("add=" + add);
System.out.println("subtract=" + subtract);
System.out.println("multiply=" + multiply);
System.out.println("divide=" + divide);
取余
BigInteger mod = b1.mod(b2);
System.out.println("mod=" + mod);
同时获取商和余数
BigInteger m = BigInteger.valueOf(110);
BigInteger n = BigInteger.valueOf(100);
BigInteger[] res = m.divideAndRemainder(n);
System.out.println("商=" + res[0]);//商=1
System.out.println("余数" + res[1]);//余数10
2、进制转换
BigInteger hex = new BigInteger("FF", 16);
System.out.println("hex=" + hex);//hex=255
System.out.println("16进制转2进制=" + hex.toString(2));//16进制转2进制=11111111
3、比较和排序
Biglnteger提供了方法用于比较两个整数的大小,返回值用来判断大小关系
if (b1.compareTo(b2) > 0) {
System.out.println("b1>b2");
} else if (b1.compareTo(b2) == 0) {
System.out.println("b1=b2");
} else {
System.out.println("b1<b2");
}
4、位操作
Biglnteger支持位操作,例如左移、右移以及按位与、或、异或等操作。
BigInteger a = BigInteger.valueOf(10);
BigInteger left1Bit = a.shiftLeft(1);
BigInteger right1Bit = a.shiftRight(1);
System.out.println("left1Bit=" + left1Bit);//left1Bit=20
System.out.println("right1Bit=" + right1Bit);//right1Bit=5
//把布尔数组压缩成一个二进制数,二进制数从低到高第 i 位是 0,表示布尔数组的第 i 个元素是 false;
//从低到高第 i 位是 1,表示布尔数组的第 i 个元素是 true。
//转移方程等价于,把 f 中的每个比特位增加 x=nums[i],即左移 x 位,然后跟原来 f 计算 OR。
//前者对应选 x,后者对应不选 x。
//判断 f[s] 是否为 true,等价于判断 f 的第 s 位是否为 1,即 (f >> s & 1) == 1。
public static boolean canPartition(int[] nums) {
int s = 0;
for (int x : nums) {
s += x;
}
if (s % 2 != 0) {
return false;
}
s /= 2;
BigInteger f = BigInteger.ONE;
for (int x : nums) {
f = f.or(f.shiftLeft(x)); // f |= f << x;
}
return f.testBit(s); // 判断 f 中第 s 位是否为 1
}
5、定义常量
对于常用的常量值,可以预先定义为Biglnteger类型,避免多次创建实例的开销。
public static final BigInteger TEN = BigInteger.valueOf(10);
6、应用场景
6.1、科学计算
在科学计算中,经常需要处理超大或超小的数字。在这种情况下,BigInteger是一个理想的选择。
6.2、密码学
在密码学中,许多算法(如RSA算法)依赖大数运算。BigInteger提供了必要的大数支持,使得这些密码算法能够高效地实现。
6.3、大数据处理
在处理大数据时,可能会遇到进行精确计算的情况,例如在金融应用中处理货币计算。
1、BigInteger与Integer、Long的比较?
- 精度:Integer 和 Long 的精度有限,而 BigInteger 可以表示任意大小的整数。
- 性能:BigInteger 的运算速度比 int 和 long 慢,因为它需要处理更多的细节。
- 应用场景:Integer 和 Long 适用于小范围的整数运算,而 Biginteger 适用于需要高精度的计算。
2、BigInteger与BigDecimal的比较?
- 数据类型:BigInteger 用于处理整数,而 BigDecimal 用于处理浮点数.
- 精度管理:BigDecimal 可以精确控制小数位,而 BigInteger 则只处理整数。
- 使用场景:在需要高精度的金融计算中, BigDecimal 是更合适的选择,而在需要处理大整数时,BigInteger则更为合适。
再小的努力,乘以365都很明显!
每天⽤⼼记录⼀点点。内容也许不重要,但习惯很重要!
一个程序员最重要的能力是:写出高质量的代码!!
有道无术,术尚可求也,有术无道,止于术。
无论你是年轻还是年长,所有程序员都需要记住:时刻努力学习新技术,否则就会被时代抛弃!