Java中序列化的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2232759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:02:24  来源:igfitidea点击:

What is the purpose of Serialization in Java?

javaoopobjectserializationstream

提问by m_a_khan

I have read quite a number of articles on Serialization and how it is so nice and great but none of the arguments were convincing enough. I am wondering if someone can really tell me what is it that we can really achieve by serializing a class?

我已经阅读了很多关于 Serialization 的文章,以及它如何如此出色和出色,但没有一个论点足够令人信服。我想知道是否有人真的可以告诉我通过序列化一个类我们可以真正实现什么?

采纳答案by Schmelter

Let's define serialization first, then we can talk about why it's so useful.

我们先定义序列化,然后再说说它为什么这么有用。

Serialization is simply turning an existing object into a byte array. This byte array represents the class of the object, the version of the object, and the internal state of the object. This byte array can then be used between JVM's running the same code to transmit/read the object.

序列化只是将现有对象转换为字节数组。这个字节数组表示对象的类、对象的版本和对象的内部状态。然后可以在运行相同代码的 JVM 之间使用该字节数组来传输/读取对象。

Why would we want to do this?

我们为什么要这样做?

There are several reasons:

有几个原因:

  • Communication: If you have two machines that are running the same code, and they need to communicate, an easy way is for one machine to build an object with information that it would like to transmit, and then serialize that object to the other machine. It's not the best method for communication, but it gets the job done.

  • Persistence: If you want to store the state of a particular operation in a database, it can be easily serialized to a byte array, and stored in the database for later retrieval.

  • Deep Copy: If you need an exactreplica of an Object, and don't want to go to the trouble of writing your own specialized clone() class, simply serializing the object to a byte array, and then de-serializing it to another object achieves this goal.

  • Caching: Really just an application of the above, but sometimes an object takes 10 minutes to build, but would only take 10 seconds to de-serialize. So, rather than hold onto the giant object in memory, just cache it out to a file via serialization, and read it in later when it's needed.

  • Cross JVM Synchronization: Serialization works across different JVMs that may be running on different architectures.

  • 通信:如果您有两台机器运行相同的代码,并且它们需要通信,一个简单的方法是让一台机器构建一个包含它想要传输的信息的对象,然后将该对象序列化到另一台机器。这不是最好的沟通方法,但它可以完成工作。

  • 持久性:如果要将特定操作的状态存储在数据库中,可以很容易地将其序列化为字节数组,并存储在数据库中供以后检索。

  • 深度复制:如果您需要对象的精确副本,并且不想麻烦编写自己的专用 clone() 类,只需将对象序列化为字节数组,然后将其反序列化为另一个对象实现了这个目标。

  • 缓存:实际上只是上述的一个应用程序,但有时一个对象需要 10 分钟来构建,但反序列化只需要 10 秒。因此,与其将巨大的对象留在内存中,不如通过序列化将其缓存到文件中,然后在需要时再读入。

  • 跨 JVM 同步:序列化在可能运行在不同架构上的不同 JVM 之间工作。

回答by ddyer

The most obvious is that you can transmit the serialized class over a network, and the recepient can construct a duplicate of the original instanstance. Likewise, you can save a serialized structure to a file system.

最明显的是,您可以通过网络传输序列化的类,而接收者可以构建原始实例的副本。同样,您可以将序列化结构保存到文件系统。

Also, note that serialization is recursive, so you can serialize an entire heterogenous data structure in one swell foop, if desired.

另请注意,序列化是递归的,因此如果需要,您可以在一个 swell foop 中序列化整个异构数据结构。

回答by Pool

In essense:

本质上

Serialization is the process of converting a set of object instances that contain references to each other into a linear stream of bytes, which can then be sent through a socket, stored to a file, or simply manipulated as a stream of data

序列化是将一组包含相互引用的对象实例转换为线性字节流的过程,然后可以通过套接字发送、存储到文件或简单地作为数据流进行操作

See uses from Wiki:

请参阅来自Wiki 的用途:

Serialization has a number of advantages. It provides:

序列化有许多优点。它提供:

  1. a method of persisting objects which is more convenient than writing their properties to a text file on disk, and re-assembling them by reading this back in.
  2. a method of issuing remote procedure calls, e.g., as in SOAP
  3. a method for distributing objects, especially in software componentry such as COM, CORBA, etc.
  4. a method for detecting changes in time-varying data.
  1. 一种持久化对象的方法,比将它们的属性写入磁盘上的文本文件并通过读回重新组装它们更方便。
  2. 发出远程过程调用的方法,例如在 SOAP 中
  3. 一种分发对象的方法,尤其是在 COM、CORBA 等软件组件中。
  4. 一种检测时变数据变化的方法。

回答by David Soroko

Serialized objects maintain state in space, they can be transferred over the network, file system, etc... and time, they can outlive the JVM that created them.

序列化对象在空间中保持状态,它们可以通过网络、文件系统等进行传输……并且随着时间的推移,它们可以比创建它们的 JVM 寿命更长。

Sometimes this is useful.

有时这很有用。

回答by Gordon Gustafson

While you're running your application, all of its objects are stored in memory (RAM). When you exit, that memory gets reclaimed by the operating system, and your program essentially 'forgets' everything that happened while it was running. Serialization remedies this by letting your application save objects to disk so it can read them back the next time it starts. If your application is going to provide any way of saving/sharing a previous state, you'll need some form of serialization.

当您运行应用程序时,其所有对象都存储在内存 (RAM) 中。当您退出时,该内存会被操作系统回收,您的程序基本上会“忘记”运行时发生的所有事情。序列化通过让您的应用程序将对象保存到磁盘来解决这个问题,以便它可以在下次启动时读取它们。如果您的应用程序将提供任何保存/共享先前状态的方式,您将需要某种形式的序列化。

回答by Rushdi Shams

I can share my story and I hope it will give some ideas why serialization is necessary. However, the answers to your question are already remarkably detail.

我可以分享我的故事,我希望它能提供一些为什么需要连载的想法。但是,您的问题的答案已经非常详细。

I had several projects that need to load and read a bunch of text files. The files contained stop words, biomedical verbs, biomedical abbreviations, words semantically connected to each other, etc. The contents of these files are simple: words!

我有几个项目需要加载和读取一堆文本文件。这些文件包含停用词、生物医学动词、生物医学缩写、语义上相互关联的词等。这些文件的内容很简单:

Now for each project, I needed to read the words from each of these files and put them into different arrays; as the contents of the file never changed, it became a common, however redundant, task after the first project.

现在对于每个项目,我需要从每个文件中读取单词并将它们放入不同的数组中;由于文件的内容从未改变,因此在第一个项目之后,它变成了一项常见但多余的任务。

So, what I did is that I created one object to read each of these files and to populate individual arrays (instance variables of the objects). Then I serialized the objects and then for the later projects, I simply deserialized them. I didn't have to read the files and populate the arrays again and again.

所以,我所做的是创建一个对象来读取这些文件中的每一个并填充单个数组(对象的实例变量)。然后我序列化了对象,然后对于以后的项目,我只是反序列化了它们。我不必一次又一次地读取文件并填充数组。

回答by Ron Norman

I use serialized objects to standardize the arguments I pass to functions or class constructors. Passing one serialized bean is much cleaner than a long list of arguments. The result is code that is easier to read and debug.

我使用序列化对象来标准化我传递给函数或类构造函数的参数。传递一个序列化的 bean 比一长串参数要简洁得多。结果是更易于阅读和调试的代码。

回答by JCoder

For the simple purpose of learning (notice, I said learning, I did not say best, or even good, but just for the sake of understanding stuff), you could save your data to a text file on the computer, then have a program that reads that info, and based on the file, you could have your program respond differently. If you were more advanced, it wouldn't necessarily have to be a txt file, but something else.

出于学习的简单目的(注意,我说的是学习,我没有说最好,甚至没有说好的,只是为了理解东西),您可以将数据保存到计算机上的文本文件中,然后有一个程序读取该信息,并根据文件,您可以让您的程序做出不同的响应。如果你更高级,它不一定是一个 txt 文件,而是其他的东西。

Serializing on the other hand, puts things directly into computer language. It's like you're telling a Spanish computer something in Spanish, rather than telling it something in French, forcing it to learn French, then save things into its native Spanish by translating everything. Not the most tech-intensive answer, I'm just trying to create an understandable example in a common language format.

另一方面,序列化将事物直接放入计算机语言中。这就像你用西班牙语告诉一台西班牙计算机一些东西,而不是用法语告诉它一些东西,强迫它学习法语,然后通过翻译所有东西将东西保存成它的母语西班牙语。不是技术含量最高的答案,我只是想以通用语言格式创建一个易于理解的示例。

Serialization is also faster, because in Java, objects are handled on the heap, and take much longer than if they were represented as primitives on the stack. Speed, speed, speed. And less file processing from a programmer point of view.

序列化也更快,因为在 Java 中,对象是在堆上处理的,并且比将它们表示为堆栈上的原语花费的时间要长得多。速度,速度,速度。从程序员的角度来看,文件处理更少。