什么是Java中的序列化? [英] What is serialization in Java?

查看:149
本文介绍了什么是Java中的序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

什么是对象序列化?

我做了一个小RSS使用Swing和Eclipse的Reader应用程序一直告诉我可序列化类MochaRSSView没有声明类型为long的静态最终serialVersionUID字段

I've made a small RSS Reader app using Swing and Eclipse keeps telling me "The serializable class MochaRSSView does not declare a static final serialVersionUID field of type long"

什么是序列化以及它有什么好处? ?

What is serialization and what benefits would it have?

推荐答案

Serializable是一个标记接口,它告诉JVM它可以将对象的状态写出到某些流(基本上是读取的)所有成员,并将他们的状态写入流,或磁盘或其他东西)。默认机制是二进制格式。你也可以用它来克隆东西,或者在调用之间保持状态,通过网络发送对象等。

Serializable is a marker interfaces that tells the JVM it can write out the state of the object to some stream (basically read all the members, and write out their state to a stream, or to disk or something). The default mechanism is a binary format. You can also use it to clone things, or keep state between invocations, send objects across the network etc.

你可以让eclipse为你生成一个(基本上只是一个长的)随机但唯一的ID)。这意味着您可以控制何时认为类与序列化版本兼容。

You can let eclipse generate one for you (basically just a long random but unique ID). That means you can control when you think a class would be compatible with a serialized version, or not.

(注意:所有非瞬态成员变量必须是可序列化的类,否则您将收到错误 - 因为JVM将通过结构写出来每个对象的状态,直到将原语写入ObjectOutputStream的水平。)

(Note: that all the non transient member variables must be of a serializable class, or you will get an error - as the JVM will recurse through the structure writing out the state of each object down to the level of writing primitives to the ObjectOutputStream).

这篇关于什么是Java中的序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆