如果在操作的時候一個整型數據已經超過了整數的最大類型長度long的話,則此數據就無法裝入,所以,此時要使用BigInteger類進行操作。
package com.BigNumber; import java.math.BigInteger; /** * @ClassName BigIntegerTest * @Description BigInteger測試 * @Author Administrator * @Date 2019/5/29 21:04 * @Version 1.0 **/ public class BigIntegerTest { public static void main(String[] args) { BigInteger bi1 = new BigInteger("123456781"); BigInteger bi2 = new BigInteger("987654321"); System.out.println(bi1.add(bi2));//加法操作 System.out.println(bi1.subtract(bi2));//減法操作 System.out.println(bi1.multiply(bi2));//乘法操作 System.out.println(bi1.divide(bi2));//除法操作 } }
底層原理:
BigInteger的底層是用int類型的數組存儲的
在初始化的過程中,只要包括了判斷這個數的正負性,找到第一個非0的字符等。