think in java - IO - object serialization

# why we need object serialization?

- There are situations in which it would be incredibly useful if an object could exist and hold its information even when the program isn't running.

- We can get similar effect by writing the information to a file or to a DB.

- in the spirit of making everything an object, it would be quite convenient to declare an object to be "persistent"


# what is object serialization?

which allows you to take any object that implements Serializable interface and turn it into a sequence of bytes that can be later fully restored to regenerate the original object.

Object serialization was added to java to support 2 features: RMI && JavaBean.


# Seriablable vs Externalizable

by implementating java.io.Serializable, you get "automatic" serialization capability for objects of your class. No need to implement any other logic, it'll just work. The Java runtime will use reflection to figure out how to marshal and unmarshal your objects.

In earlier version of Java, reflection was very slow, and so serializaing large object graphs (e.g. in client-server RMI applications) was a bit of a performance problem. To handle this situation, the java.io.Externalizable interface was provided, which is like java.io.Serializable but with custom-written mechanisms to perform the marshalling and unmarshalling functions (you need to implement readExternal and writeExternal methods on your class). This gives you the means to get around the reflection performance bottleneck.


转载于:https://my.oschina.net/u/1242229/blog/286597

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值