java解惑之大问题

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的方法:

BigInteger add(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 an int.

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啦!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值