java解惑之大問題

來源:互聯網
上載者:User

標籤: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解惑之大問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.