一文讓你徹底學(xué)會Java之BigInteger類常用操作
BigInteger 的基本概念
1. 什么是 BigInteger
BigInteger
是java.math
包中的類,專門用于表示任意精度的整數(shù)。- 它解決了基本數(shù)據(jù)類型(如
int
、long
)的精度限制問題。比如:int
的最大值是 2³¹-1(21 億)。long
的最大值是 2?³-1(約 19 位十進(jìn)制數(shù))。- 如果需要表示更大的整數(shù),就必須使用
BigInteger
。
2. 特點
- 不可變性:
BigInteger
是不可變對象,每次運算都會返回新的實例,而不會修改原有對象。
- 任意精度:
- 只受限于可用內(nèi)存的大小,因此可以表示超大整數(shù)。
- 底層實現(xiàn):
BigInteger
底層采用int[]
數(shù)組存儲數(shù)據(jù),每個數(shù)組元素存儲一部分?jǐn)?shù)字,并通過數(shù)學(xué)算法進(jìn)行操作。- 例如,將一個非常大的數(shù)字按 32 位一段分開存儲。
BigInteger 的構(gòu)造方式
構(gòu)造函數(shù)
BigInteger
提供了多個構(gòu)造器用于創(chuàng)建實例。
1. 使用字符串構(gòu)造
這是最常用的構(gòu)造方法,支持任意大小的數(shù)字:
BigInteger bigInt = new BigInteger("123456789012345678901234567890");
- 參數(shù):
- 字符串表示的數(shù)字(支持正負(fù)號)。
- 如果字符串中包含非法字符(如字母),會拋出
NumberFormatException
。
2. 使用基本數(shù)據(jù)類型的轉(zhuǎn)換
通過靜態(tài)方法 BigInteger.valueOf
將 int
或 long
轉(zhuǎn)換為 BigInteger
:
BigInteger bigInt = BigInteger.valueOf(123456789L);
- 注意:
valueOf
方法只支持long
范圍內(nèi)的數(shù)值。- 對于更大的數(shù)字,仍需使用字符串構(gòu)造。
3. 從二進(jìn)制、八進(jìn)制、十六進(jìn)制等進(jìn)制構(gòu)造
支持指定進(jìn)制的字符串構(gòu)造:
BigInteger bigInt = new BigInteger("1010", 2); // 二進(jìn)制 -> 十進(jìn)制:10 BigInteger hexInt = new BigInteger("1A", 16); // 十六進(jìn)制 -> 十進(jìn)制:26
4. 生成隨機數(shù)
通過構(gòu)造一個隨機數(shù):
BigInteger randomBigInt = new BigInteger(50, new Random()); // 生成 50 位隨機數(shù)
- 參數(shù)含義:
- 第一個參數(shù)是位數(shù)(bit 數(shù))。
- 第二個參數(shù)是隨機數(shù)生成器。
5. 常用常量
BigInteger.ZERO // 表示 0 BigInteger.ONE // 表示 1 BigInteger.TEN // 表示 10
BigInteger 的常用操作
1. 算術(shù)運算
BigInteger a = new BigInteger("12345"); BigInteger b = new BigInteger("67890");
加法
BigInteger sum = a.add(b); // 結(jié)果:80235
減法
BigInteger diff = a.subtract(b); // 結(jié)果:-55545
乘法
BigInteger product = a.multiply(b); // 結(jié)果:838102050
除法
BigInteger quotient = b.divide(a); // 結(jié)果:5
取模
BigInteger mod = b.remainder(a); // 結(jié)果:12345
- 注意:
divide
是整數(shù)除法,不會產(chǎn)生小數(shù)部分。
混合運算:加法、乘法
所有的運算都是鏈?zhǔn)降?。例如?/p>
BigInteger result = a.add(b).multiply(a).divide(b);
2. 冪運算
- 普通冪運算(不取模):
BigInteger base = new BigInteger("2"); BigInteger result = base.pow(10); // 2^10 = 1024
- 模冪運算(大數(shù)運算中常用):
BigInteger base = new BigInteger("2"); BigInteger exp = new BigInteger("10"); BigInteger mod = new BigInteger("7"); BigInteger modPowResult = base.modPow(exp, mod); // (2^10) % 7 = 2
底層優(yōu)化:
- 模冪運算采用了指數(shù)平方算法,時間復(fù)雜度為 O(log(n)) ,比逐步計算效率更高。
3. 比較和最大最小值
比較大小
compareTo
方法返回以下結(jié)果:
1
:當(dāng)前對象大于比較對象。-1
:當(dāng)前對象小于比較對象。0
:兩者相等。
示例:
int compare = a.compareTo(b);
最大值和最小值
- 返回兩個數(shù)中的較大或較小值:
BigInteger max = a.max(b); BigInteger min = a.min(b);
4. 位運算
按位操作
- 按位與:
BigInteger result = a.and(b);
- 按位或:
BigInteger result = a.or(b);
- 按位異或:
BigInteger result = a.xor(b);
位移操作
- 左移:
BigInteger result = a.shiftLeft(2); // 左移 2 位
- 右移:
BigInteger result = a.shiftRight(2); // 右移 2 位
5. 數(shù)學(xué)相關(guān)
求絕對值
BigInteger abs = a.abs();
求最大公約數(shù)
BigInteger gcd = a.gcd(b); // 返回 a 和 b 的最大公約數(shù)
模反元素
模反元素是滿足以下公式的整數(shù):
( a × x ) % m = 1 (a \times x) \% m = 1(a×x)%m=1
代碼示例:
BigInteger modInverse = a.modInverse(mod); // 計算 a 的模反元素
檢查是否是質(zhì)數(shù)
boolean isPrime = a.isProbablePrime(10); // 參數(shù)為測試的可信度
- 參數(shù)是一個整數(shù),值越大,測試結(jié)果的可靠性越高。
6. 進(jìn)制轉(zhuǎn)換
- 轉(zhuǎn)為字符串(默認(rèn)十進(jìn)制):
String decimalStr = a.toString();
- 轉(zhuǎn)為其他進(jìn)制表示:
String binaryStr = a.toString(2); // 二進(jìn)制 String hexStr = a.toString(16); // 十六進(jìn)制
優(yōu)化
1. 不可變性
BigInteger
的每次操作都會返回一個新對象。如果需要頻繁修改值,可以考慮復(fù)用變量以減少對象創(chuàng)建。
2. 注意內(nèi)存消耗
超大整數(shù)會消耗較多內(nèi)存。例如:
BigInteger big = new BigInteger("9".repeat(1000000)); // 100 萬位數(shù)字
總結(jié)
到此這篇關(guān)于Java之BigInteger類常用操作的文章就介紹到這了,更多相關(guān)Java BigInteger類常用操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java8函數(shù)式接口Predicate用法示例詳解
這篇文章主要為大家介紹了Java8函數(shù)式接口Predicate用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07SpringBoot整合EasyExcel實現(xiàn)復(fù)雜Excel表格的導(dǎo)入導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何整合EasyExcel實現(xiàn)復(fù)雜Excel表格的導(dǎo)入導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考下2023-11-11SpringCloud Zuul在何種情況下使用Hystrix及問題小結(jié)
這篇文章主要介紹了SpringCloud Zuul在何種情況下使用Hystrix 及問題小結(jié),感興趣的朋友跟隨小編一起看看吧2018-11-11