码迷,mamicode.com
首页 > 编程语言 > 详细

java解惑之大问题

时间:2015-04-16 21:56:13      阅读:139      评论:0      收藏:0      [点我收藏+]

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

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

java解惑之大问题

标签:java解惑   biginteger   

原文地址:http://blog.csdn.net/havedream_one/article/details/45080655

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!