深入Java不可變類型的詳解
更新時間:2013年06月04日 17:05:19 作者:
本篇文章是Java中的不可變類型進行了詳細的分析介紹,需要的朋友參考下
我們先看下面一個例子:
import java.math.BigInteger;
public class BigProblem {
public static void main(String[ ] args) {
BigInteger fiveThousand = new BigInteger("5000");
BigInteger fiftyThousand = new BigInteger("50000");
BigInteger fiveHundredThousand = new BigInteger("500000");
BigInteger total = BigInteger.ZERO;
total.add(fiveThousand);
total.add(fiftyThousand);
total.add(fiveHundredThousand);
System.out.println(total);
}
}
你可能會認為這個程序會打印出555000。畢竟,它將total設(shè)置為用BigInteger表示的0,然后將5,000、50,000和500,000加到了這個變量上。如果你運行該程序,你就會發(fā)現(xiàn)它打印的不是555000,而是0。很明顯,所有這些加法對total沒有產(chǎn)生任何影響。
對此有一個很好理由可以解釋:BigInteger實例是不可變的。String、BigDecimal以及包裝器類型:Integer、Long、Short、Byte、Character、Boolean、Float和Double也是如此,你不能修改它們的值。我們不能修改現(xiàn)有實例的值,對這些類型的操作將返回新的實例。起先,不可變類型看起來可能很不自然,但是它們具有很多勝過與其向?qū)?yīng)的可變類型的優(yōu)勢。不可變類型更容易設(shè)計、實現(xiàn)和使用;它們出錯的可能性更小,并且更加安全[EJ Item 13]。
為了在一個包含對不可變對象引用的變量上執(zhí)行計算,我們需要將計算的結(jié)果賦值給該變量。這樣做就會產(chǎn)生下面的程序,它將打印出我們所期望的555000:
import java.math.BigInteger;
public class BigProblem {
public static void main(String[] args) {
BigInteger fiveThousand = new BigInteger("5000");
BigInteger fiftyThousand = new BigInteger("50000");
BigInteger fiveHundredThousand = new BigInteger("500000");
BigInteger total = BigInteger.ZERO;
total = total.add(fiveThousand);
total = total.add(fiftyThousand);
total = total.add(fiveHundredThousand);
System.out.println(total);
}
}
復(fù)制代碼 代碼如下:
import java.math.BigInteger;
public class BigProblem {
public static void main(String[ ] args) {
BigInteger fiveThousand = new BigInteger("5000");
BigInteger fiftyThousand = new BigInteger("50000");
BigInteger fiveHundredThousand = new BigInteger("500000");
BigInteger total = BigInteger.ZERO;
total.add(fiveThousand);
total.add(fiftyThousand);
total.add(fiveHundredThousand);
System.out.println(total);
}
}
你可能會認為這個程序會打印出555000。畢竟,它將total設(shè)置為用BigInteger表示的0,然后將5,000、50,000和500,000加到了這個變量上。如果你運行該程序,你就會發(fā)現(xiàn)它打印的不是555000,而是0。很明顯,所有這些加法對total沒有產(chǎn)生任何影響。
對此有一個很好理由可以解釋:BigInteger實例是不可變的。String、BigDecimal以及包裝器類型:Integer、Long、Short、Byte、Character、Boolean、Float和Double也是如此,你不能修改它們的值。我們不能修改現(xiàn)有實例的值,對這些類型的操作將返回新的實例。起先,不可變類型看起來可能很不自然,但是它們具有很多勝過與其向?qū)?yīng)的可變類型的優(yōu)勢。不可變類型更容易設(shè)計、實現(xiàn)和使用;它們出錯的可能性更小,并且更加安全[EJ Item 13]。
為了在一個包含對不可變對象引用的變量上執(zhí)行計算,我們需要將計算的結(jié)果賦值給該變量。這樣做就會產(chǎn)生下面的程序,它將打印出我們所期望的555000:
復(fù)制代碼 代碼如下:
import java.math.BigInteger;
public class BigProblem {
public static void main(String[] args) {
BigInteger fiveThousand = new BigInteger("5000");
BigInteger fiftyThousand = new BigInteger("50000");
BigInteger fiveHundredThousand = new BigInteger("500000");
BigInteger total = BigInteger.ZERO;
total = total.add(fiveThousand);
total = total.add(fiftyThousand);
total = total.add(fiveHundredThousand);
System.out.println(total);
}
}
相關(guān)文章
Maven項src/main/java目錄下配置文件無法被導(dǎo)出或者生效的問題和處理方案
這篇文章主要介紹了Maven項src/main/java目錄下配置文件無法被導(dǎo)出或者生效的問題和處理方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Windows 10上JDK環(huán)境安裝配置圖文教程
這篇文章主要為大家詳細介紹了Windows 10上JDK環(huán)境安裝配置圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03Java數(shù)據(jù)結(jié)構(gòu)之快速冪的實現(xiàn)
快速冪是用來解決求冪運算的高效方式。本文將詳細為大家介紹如何利用Java實現(xiàn)快速冪,以及利用快速冪求解冪運算問題,需要的可以參考一下2022-03-03Java class文件格式之屬性詳解_動力節(jié)點java學(xué)院整理
這篇文章主要介紹了Java class文件格式之屬性詳解,需要的朋友可以參考下2017-06-06