Java mutable對象和immutable對象的區(qū)別說明
今天讀jdk源碼中Map.java時看到一句話:
great care must be exercised if mutable objects are used as map keys;
第一次知道m(xù)utable對象這個概念,google了一下,維基百科定義如下:
“In object-oriented and functional programming, an immutable object (unchangeable[1] object) is an object whose state cannot be modified after it is created.[2] This is in contrast to a mutable object (changeable object) , which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change but the object’s state appears to be unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.”
在面向?qū)ο蠛秃瘮?shù)式編程中,一個immutable對象(不可變對象)是指一旦創(chuàng)建之后狀態(tài)不可改變的對象。
mutable對象(可變對象)是指創(chuàng)建之后也可以修改的對象。
在有些情況下,對象也被認為是不可變的(immutable),即,一個對象包含的內(nèi)部使用的屬性改變了,但從外部看對象的狀態(tài)并沒有改變。
例如,一個使用memoization來緩存復(fù)雜計算結(jié)果的對象仍然被看作是不可變(immutable)對象.
在面向?qū)ο缶幊讨?,String 以及其他的具體對象都被看作是不可變(immutable)對象,以提高可讀性和運行效率。
不可變對象有幾個優(yōu)點:線程安全
易于理解
比可變對象有更高的安全性
Java中不可變對象的經(jīng)典例子就是String類的實例:String s = 'ABC';s.toLowerCase();
toLowerCase()方法不會改變s中包含的數(shù)據(jù)“ABC”。而是創(chuàng)建一個新的String對象并將其初始化為“abc”,然后返回這個新對象的引用。
盡管String類聲明中沒有提供讓它成為不可變對象的語法,但是,String類的方法中沒有方法去改變一個String包含的數(shù)據(jù),這就使得它是不可變的。
Java中關(guān)鍵字final用于聲明原始數(shù)據(jù)類型(primitive types)和對象引用為不可變對象,但是它不能使對象本身變?yōu)椴豢勺儗ο蟆?/p>
原始數(shù)據(jù)類型(primitive types)變量(int, long, short等)定義之后還可以再重新賦值,可以使用final阻止這樣的賦值。
int i = 42; //int is of primitive typei = 43; // OKfinal int j = 42;j = 43; // does not compile. j is final so can’t be reassigned
僅僅使用final關(guān)鍵字還不能讓引用類型(reference types)成為不可變對象,final只能阻止重新賦值。
final MyObject m = new MyObject(); //m is of reference typem.data = 100; // OK. We can change state of object m (m is mutable and final doesn’t change this fact)m = new MyObject(); // does not compile. m is final so can’t be reassigned
原始類型包裝類(primitive wrappers)(Integer,Long, Short, Double, Float, Character, Byte, Boolean)也都是不可變的。
Java mutable 和 immutable類型含義解釋
immutable : variables that are assigned once and never reassigned.
mutable : When you assign to a variable or a field, you’re changing where the variable’s arrow points. You can point it to a different value. When you assign to the contents of a mutable value ? such as an array or list ? you’re changing references inside that value.
基本類型及其封裝對象類型都是不可變的
圖形化解釋 Snapshot Diagrammutable:
immutable:
例如String和StringBuilder:
1. String是immutable的,每次對于String對象的修改都將產(chǎn)生一個新的String對象,而原來的對象保持不變。
2. StringBuilder是mutable,因為每次對于它的對象的修改都作用于該對象本身,并沒有產(chǎn)生新的對象。
如何保證自己創(chuàng)建的類是immutable類 所有成員都是private final。 不提供對成員的改變方法,setXX 確保所有的方法不會被重寫。手段有兩種:使用final Class(強不可變類),或者將所有類方法加上final(弱不可變類)。 如果某一個類成員不是原始變量(例如int,double)或者不可變類,必須通過在成員初始化或者使用get方法時要通過深度拷貝方法,來確保類的不可變。優(yōu)缺點使用不可變類型,對其頻繁修改會產(chǎn)生大量的臨時拷貝(需要垃圾回收) ;
可變類型最 少化拷貝以提高效率。
使用可變數(shù)據(jù)類型,可獲得更好的性能 ,也適合于在多個模塊之間共享數(shù)據(jù) 。例如全局變量。
不可變類型更“安全”, 在其他質(zhì)量指標(biāo)上表現(xiàn)更好。
對可變類型可能造成的風(fēng)險,我們通過防御式拷貝(深度拷貝),給客戶端返回一個全新的可變類型的對象,大部分時候該拷貝不會被客戶端修改, 可能造成大量的內(nèi)存浪費。
深度拷貝當(dāng)只是引用傳遞或者根據(jù)對象的值創(chuàng)建新的值,稱為“淺復(fù)制”,當(dāng)原對象的發(fā)生改變時,根據(jù)上面方式創(chuàng)建的新對象的也會隨之改變;
而如果采用深度復(fù)制,那是真正的復(fù)制了一份新的對象,新對象的與原對象不存在任何關(guān)聯(lián),原對象發(fā)生改變不會影響新對象。
相關(guān)文章:
