第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

ArrayList中為什么size要序列化兩次?

在代碼中s.defaultWriteObject();中size應(yīng)該也被序列化了,為什么下邊還要再單獨(dú)序列化一次呢?

正在回答

1 回答

這樣寫是出于兼容性考慮。

舊版本的JDK中,ArrayList的實現(xiàn)有所不同,會對length字段進(jìn)行序列化。

而新版的JDK中,對優(yōu)化了ArrayList的實現(xiàn),不再序列化length字段。

這個時候,如果去掉s.writeInt(size),那么新版本JDK序列化的對象,在舊版本中就無法正確讀取,

因為缺少了length字段。

因此這種寫法看起來多此一舉,實際上卻保證了兼容性。

附上官方解釋:defaultReadObject()?and?defaultWriteObject()?should?be?the?first?method?call?inside?readObject(ObjectInputStream?o)?and?writeObject(ObjectOutputStream?o).?It?reads?and?writes?all?the?non-transient?fields?of?the?class?respectively.?

These?methods?also?helps?in?backward?and?future?compatibility.?If?in?future?you?add?some?non-transient?field?to?the?class?and?you?are?trying?to?deserialize?it?by?the?older?version?of?class?then?the?defaultReadObject()?method?will?neglect?the?newly?added?field,?similarly?if?you?deserialize?the?old?serialized?object?by?the?new?version?then?the?new?non?transient?field?will?take?default?value?from?JVM?i.e.?if?its?object?then?null?else?if?primitive?then?boolean?to?false,?int?to?0?etc….

11 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

ArrayList中為什么size要序列化兩次?

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號