java readexternal,Java中的Serializable - writeObject()/ ReadObject和Externalizable - readExternal()...

I understood from this posting that

Serializable is incredibly easy to implement, and resilient to change (in most cases all you have to do is update the serialversionUID). If we want to control of read and write process we can implement Externalizable.

If all we want is the control of read and write process, we can override the below methods for serialization right? why do we need to introduce the new interface Externalizable?

private void writeObject(java.io.ObjectOutputStream out)

throws IOException

private void readObject(java.io.ObjectInputStream in)

throws IOException, ClassNotFoundException;

private void readObjectNoData()

throws ObjectStreamException;

解决方案

You ask:

why do we need to introduce the new interface Externalizable?

The best rationale I've been able to find (in Oracle documentation) is in the WebLogic JMS Best Practice document:

"The CPU cost of serializing Java objects can be significant. This expense, in turn, affects JMS Object messages. You can offset this cost, to some extent, by having application objects implement java.io.Externalizable, but there still will be significant overhead in marshalling the class descriptor. To avoid the cost of having to write the class descriptors of additional objects embedded in an Object message, have these objects implement Externalizable, and call readExternal and writeExternal on them directly. For example, call obj.writeExternal(stream) rather than stream.writeObject(obj). Using Bytes and Stream messages is generally a preferred practice."

In short, you can get better performance using Externalizable, at least in some situations.

And "the difference" is that if you use Serializable the work of serialization is typically done for you, but with Externalizable you need to code it yourself.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值