简体   繁体   English

使类可序列化的用途是什么?

[英]What is use of making classes serializable?

  1. I have noticed many of the library classes "ArrayList", "String" even the exceptions are having a serialVersionUID. 我注意到许多库类“ArrayList”,“String”甚至异常都有一个serialVersionUID。 Why they have made it like this. 为什么他们这样做了。 Whats the practical use of doing that.FYI I am familiar with the concept of Serialization. 这样做的实际用途是什么。我很熟悉序列化的概念。 Please point out the practical purpose of it. 请指出它的实际目的。

For your reference find the serialversionUid for ClassCastException 供您参考,查找ClassCastException的serialversionUid

public class ClassCastException extends RuntimeException {
private static final long serialVersionUID = -9223365651070458532L;

Where these object's state going to persist? 这些对象的状态会持续存在吗? And where will these objects state going to be retrieved ? 这些对象将在何处被检索?

  1. I am currently working in a project where we are making REST controllers whose input and output parameters will be JSON.We are creating simple POJOs for i/p and o/p parameters.I have seen people making those POJOs serializable .Whats the point in doing that ? 我目前正在开发一个项目,我们正在制作REST控制器,其输入和输出参数将是JSON。我们正在为i / p和o / p参数创建简单的POJO。我已经看到人们将这些POJO序列化。看看是什么意思这样做?

But I havent seen **out.readObject** or out.writeobject which is used to write and read the state of object.Will the POJO's state persist just making it serializable? 但我还没有看到**out.readObject**out.writeobject用于写入和读取对象的状态。POJO的状态是否仍然只是使其可序列化? If yes where it will be stored? 如果是,它将被存储在哪里?

If you want the full story, read the spec: Java Object Serialization Specification . 如果您需要完整的故事,请阅读规范: Java对象序列化规范

[...] many of the library classes "ArrayList", "String" even the exceptions are having a serialVersionUID. [...]许多库类“ArrayList”,“String”甚至异常都有一个serialVersionUID。 Why they have made it like this. 为什么他们这样做了。

To support backwards compatibility when reading objects that were written in an older version of the class. 在读取在较旧版本的类中编写的对象时支持向后兼容性。 See Stream Unique Identifiers . 请参阅流唯一标识符

Where these object's state going to persist? 这些对象的状态会持续存在吗?

Wherever you decide. 无论你在哪里决定。 See Writing to an Object Stream . 请参阅写入对象流

And where will these objects state going to be retrieved ? 这些对象将在何处被检索?

Wherever you put it. 无论你把它放在哪里。 See Reading from an Object Stream . 请参阅从对象流中读取

[...] input and output parameters will be JSON. [...]输入和输出参数将是JSON。 [...] I have seen people making those POJOs serializable. [...]我看到人们将这些POJO序列化。 Whats the point in doing that ? 这样做的重点是什么?

None. 没有。 JSON is not using Java serialization. JSON没有使用Java序列化。 Java serialization creates a binary stream. Java序列化创建二进制流。 JSON creates text. JSON创建文本。

Will the POJO's state persist just making it serializable? POJO的状态是否会继续使其可序列化? If yes where it will be stored? 如果是,它将被存储在哪里?

No, see above. 不,见上文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM