要點:
1、BigInteger屬于java.math.BigInteger,因此在每次使用前都要import 這個類。
2、其常用的構造方法有:
BigInteger(String val) 將 BigInteger 的十進制字符串表示形式轉換為 BigInteger。 |
BigInteger(String val, int radix) 將指定基數的 BigInteger 的字符串表示形式轉換為 BigInteger。 |
3、BigInteger類模擬了所有的int型數學操作,如add()==“+”,divide()==“-”等,但注意其內容進行數學運算時不能直接使用數學運算符進行運算,必須使用其內部方法。而且其操作數也必須為BigInteger型。
如:two.add(2)就是一種錯誤的操作,因為2沒有變為BigInteger型。
4、當要把計算結果輸出時應該使用.toString方法將其轉換為10進制的字符串,詳細說明如下:
String | toString() 返回此 BigInteger 的十進制字符串表示形式。 |
5、三個經常用到的函數:
BigInteger | remainder(BigInteger val) 返回其值為 (this % val) 的 BigInteger。 |
BigInteger | negate() 返回其值是 (-this) 的 BigInteger。 |
int | compareTo(BigInteger val) 將此 BigInteger 與指定的 BigInteger 進行比較。 |
negate將操作數變為相反數。
compare的詳解如下:
compareTo
public int compareTo(BigInteger val)
將此 BigInteger 與指定的 BigInteger 進行比較。對于針對六個布爾比較運算符 (<, ==, >, >=, !=, <=) 中的每一個運算符的各個方法,優先提供此方法。執行這些比較的建議語句是:(x.compareTo(y) <op> 0),其中 <op> 是六個比較運算符之一。
指定者:接口 Comparable<BigInteger>
中的 compareTo
參數: val
- 將此 BigInteger 與之比較的 BigInteger。
返回: 當此 BigInteger 在數值上小于、等于或大于 val 時,返回 -1,0,或 1


























