標籤:java解惑 biginteger
import java.util.*;import java.math.BigInteger;public class FiftySixth{ public static void main(String[] args){ BigInteger one = new BigInteger("500"); BigInteger two = new BigInteger("5000"); BigInteger three = new BigInteger("50000"); BigInteger total = BigInteger.ZERO; total.add(one); total.add(two); total.add(three); System.out.println(total); }}
輸出多少?
0
怎麼會呢?不是加了嗎?
注意了BigInteger是不可變類型
不可變類型:
String,Integer,BigDecimal,Long,Charactor,Short,Byte,Boolean,Float,Double
再看看BigInteger的方法:
BigIntegeradd(BigInteger val)Returns a BigInteger whose value is (this + val)
BigInteger divide(BigInteger val)Returns a BigInteger whose value is(this / val).
int intValue()Converts this BigInteger to anint.
BigIntegernegate()Returns a BigInteger whose value is (-this)
BigIntegersubtract(BigInteger val)Returns a BigInteger whose value is (this - val).
可以觀察到,加減乘除的返回值都是BigInteger,這也說明最終的運算是返回結果,但是不改變原值。
也可以這樣理解
int i=0;
i+1+2+3+4+5+6;
那麼i的值是多少?當然是0啦!!!
java解惑之大問題