在Java编程中,Long类型的取值范围是-2^63到2^63-1,当需要处理超过这个范围的数值时,比如计算超大整数的阶乘、处理密码学中的大数运算场景,就需要使用java.math包下的BigInteger类来完成相关操作。BigInteger可以表示任意精度的整数,不会因为数值过大而出现溢出问题。

BigInteger的构造方式
BigInteger提供了多种构造方法,最常用的是通过字符串或者字节数组来创建实例,不建议使用long类型参数构造,因为那样还是受限于Long的取值范围。
常用构造方法示例
import java.math.BigInteger;
public class BigIntegerDemo {
public static void main(String[] args) {
// 通过字符串构造BigInteger,字符串可以是任意长度的数字
BigInteger num1 = new BigInteger("123456789012345678901234567890");
// 通过long类型构造,仅适用于不超过Long范围的数值
BigInteger num2 = BigInteger.valueOf(123456789L);
// 通过字节数组构造,字节数组表示数值的补码形式
byte[] bytes = {0x12, 0x34, 0x56, 0x78};
BigInteger num3 = new BigInteger(bytes);
System.out.println("num1: " + num1);
System.out.println("num2: " + num2);
System.out.println("num3: " + num3);
}
}
BigInteger的常用运算操作
BigInteger的运算方法都返回新的BigInteger实例,不会修改原有实例的值,因为BigInteger是不可变的类。
基础四则运算
加减乘除分别对应add、subtract、multiply、divide方法,示例如下:
import java.math.BigInteger;
public class BigIntegerCalc {
public static void main(String[] args) {
BigInteger a = new BigInteger("1000000000000000000000");
BigInteger b = new BigInteger("200000000000000000000");
// 加法
BigInteger sum = a.add(b);
// 减法
BigInteger diff = a.subtract(b);
// 乘法
BigInteger product = a.multiply(b);
// 除法,注意这里会做整除,丢弃小数部分
BigInteger quotient = a.divide(b);
System.out.println("和: " + sum);
System.out.println("差: " + diff);
System.out.println("积: " + product);
System.out.println("商: " + quotient);
}
}
取模与取余操作
BigInteger提供了mod和remainder两个方法,mod是数学意义上的取模,结果非负;remainder是取余,结果符号和被除数一致。
import java.math.BigInteger;
public class BigIntegerMod {
public static void main(String[] args) {
BigInteger a = new BigInteger("-10");
BigInteger b = new BigInteger("3");
// 取模,结果为非负
BigInteger modResult = a.mod(b);
// 取余,结果符号和被除数一致
BigInteger remainderResult = a.remainder(b);
System.out.println("mod结果: " + modResult); // 输出2
System.out.println("remainder结果: " + remainderResult); // 输出-1
}
}
比较大小操作
可以使用compareTo方法比较两个BigInteger的大小,返回值为-1、0、1,分别表示小于、等于、大于。
import java.math.BigInteger;
public class BigIntegerCompare {
public static void main(String[] args) {
BigInteger x = new BigInteger("123456789");
BigInteger y = new BigInteger("987654321");
int result = x.compareTo(y);
if (result < 0) {
System.out.println("x小于y");
} else if (result == 0) {
System.out.println("x等于y");
} else {
System.out.println("x大于y");
}
}
}
BigInteger的常用工具方法
除了基础运算,BigInteger还提供了很多实用的方法,比如求最大公约数、幂运算、判断质数等。
gcd(BigInteger val):返回当前数和参数的最大公约数pow(int exponent):返回当前数的exponent次幂isProbablePrime(int certainty):判断当前数是否为质数,certainty参数越大,判断错误的概率越低abs():返回当前数的绝对值negate():返回当前数的相反数
工具方法示例
import java.math.BigInteger;
public class BigIntegerUtil {
public static void main(String[] args) {
BigInteger num1 = new BigInteger("12");
BigInteger num2 = new BigInteger("18");
// 求最大公约数,结果为6
BigInteger gcd = num1.gcd(num2);
// 求2的100次幂
BigInteger powResult = BigInteger.valueOf(2).pow(100);
// 判断1000000007是否为质数,certainty设为10,错误概率极低
boolean isPrime = BigInteger.valueOf(1000000007).isProbablePrime(10);
System.out.println("最大公约数: " + gcd);
System.out.println("2的100次幂: " + powResult);
System.out.println("1000000007是否为质数: " + isPrime);
}
}
使用BigInteger的注意事项
BigInteger是不可变类,所有运算操作都会返回新的实例,频繁运算时会产生较多对象,对性能有一定影响,非必要场景不要滥用。
另外,BigInteger的除法运算如果无法整除,会直接丢弃小数部分,如果需要保留小数,需要结合BigDecimal类使用。同时,构造BigInteger时如果传入的字符串包含非数字字符,会抛出NumberFormatException异常,需要做异常处理。
当需要将BigInteger转换为基本数据类型时,要注意数值范围,比如转换为int时如果数值超过int范围,会抛出ArithmeticException异常,可以使用intValueExact、longValueExact等方法,这些方**在转换溢出时主动抛出异常,方便排查问题。
import java.math.BigInteger;
public class BigIntegerConvert {
public static void main(String[] args) {
BigInteger bigNum = new BigInteger("123");
// 安全转换为int,数值在int范围内不会抛异常
int intVal = bigNum.intValueExact();
BigInteger overNum = new BigInteger("9999999999999");
try {
// 数值超过int范围,会抛出异常
int overInt = overNum.intValueExact();
} catch (ArithmeticException e) {
System.out.println("数值超过int范围,转换失败");
}
}
}
BigIntegerLongJava大整数数值运算修改时间:2026-07-06 23:30:32