日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java的transient关键字

發布時間:2025/3/16 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java的transient关键字 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在關于 java的集合類的學習中,我們發現ArrayList類和Vector類都是使用數組實現的,但是在定義數組elementData這個屬性時稍有不同,那就是ArrayList使用transient關鍵字


private transient Object[] elementData;protected Object[] elementData;
那么,首先我們來看一下transient關鍵字的作用是什么。

transient

?????? Java語言的關鍵字,變量修飾符,如果用transient聲明一個實例變量,當對象存儲時,它的值不需要維持。這里的對象存儲是指,Java的serialization提供的一種持久化對象實例的機制。當一個對象被序列化的時候,transient型變量的值不包括在序列化的表示中,然而非transient型的變量是被包括進去的。使用情況是:當持久化對象時,可能有一個特殊的對象數據成員,我們不想用serialization機制來保存它。為了在一個特定對象的一個域上關閉serialization,可以在這個域前加上關鍵字transient。

ArrayList使用了transient關鍵字進行存儲優化,而Vector沒有這樣做,為什么?

ArrayList

/*** Save the state of the <tt>ArrayList</tt> instance to a stream (that* is, serialize it).** @serialData The length of the array backing the <tt>ArrayList</tt>* instance is emitted (int), followed by all of its elements* (each an <tt>Object</tt>) in the proper order.*/private void writeObject(java.io.ObjectOutputStream s)throws java.io.IOException{// Write out element count, and any hidden stuffint expectedModCount = modCount;s.defaultWriteObject();// Write out array lengths.writeInt(elementData.length);// Write out all elements in the proper order.for (int i=0; i<size; i++)s.writeObject(elementData[i]);if (modCount != expectedModCount) {throw new ConcurrentModificationException();}}
ArrayList實現了writeObject方法,可以看到只保存了非null的數組位置上的數據。即list的size個數的elementData。需要額外注意的一點是,ArrayList的實現,提供了fast-fail機制,可以提供弱一致性。

Vector

/** * Save the state of the {@code Vector} instance to a stream (that * is, serialize it). * This method performs synchronization to ensure the consistency * of the serialized data. */private void writeObject(java.io.ObjectOutputStream s)throws java.io.IOException {final java.io.ObjectOutputStream.PutField fields = s.putFields();final Object[] data;synchronized (this) {fields.put("capacityIncrement", capacityIncrement);fields.put("elementCount", elementCount);data = elementData.clone();}fields.put("elementData", data);s.writeFields();}

Vector也實現了writeObject方法,但方法并沒有像ArrayList一樣進行優化存儲,實現語句是
data = elementData.clone();
clone()的時候會把null值也拷貝。所以保存相同內容的Vector與ArrayList,Vector的占用的字節比ArrayList要多。

可以測試一下,序列化存儲相同內容的Vector與ArrayList,分別到一個文本文件中去。* Vector需要243字節* ArrayList需要135字節

ArrayList是非同步實現的一個單線程下較為高效的數據結構(相比Vector來說)。
ArrayList只通過一個修改記錄字段提供弱一致性,主要用在迭代器里。沒有同步方法。 即上面提到的Fast-fail機制.ArrayList的存儲結構定義為transient,重寫writeObject來實現自定義的序列化,優化了存儲。

Vector是多線程環境下更為可靠的數據結構,所有方法都實現了同步。

  • 同步處理:Vector同步,ArrayList非同步
  • Vector缺省情況下增長原來一倍的數組長度,ArrayList是0.5倍.

ArrayList: int newCapacity = oldCapacity + (oldCapacity >> 1);

ArrayList自動擴大容量為原來的1.5倍(實現的時候,方法會傳入一個期望的最小容量,若擴容后容量仍然小于最小容量,那么容量就為傳入的最小容量。擴容的時候使用的Arrays.copyOf方法最終調用native方法進行新數組創建和數據拷貝)

Vector: int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);

Vector指定了initialCapacity,capacityIncrement來初始化的時候,每次增長capacityIncrement


總結

以上是生活随笔為你收集整理的Java的transient关键字的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。