Java中的序列化是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/608647/
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-11 16:48:41  来源:igfitidea点击:

What is serialization in Java?

java

提问by danpker

Possible Duplicate:
What is object serialization?

可能的重复:
什么是对象序列化?

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"

我使用 Swing 制作了一个小型 RSS 阅读器应用程序,Eclipse 一直告诉我“可序列化类 MochaRSSView 未声明长类型的静态最终 serialVersionUID 字段”

What is serialization and what benefits would it have?

什么是序列化,它有什么好处?

采纳答案by Michael Neale

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.

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

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.

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

(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).

(注意:所有非瞬态成员变量都必须是可序列化的类,否则你会得到一个错误——因为 JVM 将通过结构递归写出每个对象的状态直到将原语写入 ObjectOutputStream 的级别) .

回答by bogertron

Serialization is writting the object into a form which is readable and allows the object to be re-created at a different time. So if I created a widget on computer A under one JVM, serialized and saved it and sent it to computer B running a different, the other JVm would be capable of de-serializing it and re-creating it with the same values and structure

序列化是将对象写入可读的形式,并允许在不同的时间重新创建对象。因此,如果我在一个 JVM 下的计算机 A 上创建一个小部件,将其序列化并保存并将其发送到运行不同的计算机 B,则另一个 JVM 将能够对其进行反序列化并使用相同的值和结构重新创建它

回答by James B

Serializationis the process of converting an object to a disk-storable format, for re-loading it at a later time. Check the link for more information!

序列化是将对象转换为磁盘可存储格式的过程,以便稍后重新加载它。查看链接以获取更多信息!

回答by Alan

Serialization is a way to take an object (an instance of your class) and set it up for transport--across a network, to disk, etc.

序列化是一种获取对象(类的实例)并将其设置为传输的方法——跨网络、到磁盘等。

回答by david a.

Serialization is process of writing an representation of object's instance to a stream (or, to a sequence of bytes). See what Sun says about it: http://java.sun.com/developer/technicalArticles/Programming/serialization/

序列化是将对象实例的表示形式写入流(或字节序列)的过程。看看 Sun 怎么说:http: //java.sun.com/developer/technicalArticles/Programming/serialization/

回答by Tom Hawtin - tackline

Java Serialisation is a way to persist object structures.

Java 序列化是一种持久化对象结构的方法。

It is best practice for serialisable class to declare serialVersionUIDas a private static final longcompile-time constant. This is used to check that object data and class code are claimed to be compatible.

将可序列化类声明serialVersionUIDprivate static final long编译时常量是最佳实践。这用于检查对象数据和类代码是否声称兼容。

So why is Eclipse telling you about this? Probably, the class that you are extending (or potentially interface you are implementing) implements java.io.Serializable. That means that all subtypes, including yours are serializable. Almost certainly you don't care. You should be able to clear the warnings by applying @SuppressWarnings("serial")on the class or package (in package-info.java). If you want to forcibly prevent instances of your class being serialised, then add (from memory):

那么为什么 Eclipse 会告诉你这个呢?可能,您正在扩展的类(或您正在实现的潜在接口)实现了java.io.Serializable. 这意味着所有子类型,包括您的子类型都是可序列化的。几乎可以肯定你不在乎。您应该能够通过应用@SuppressWarnings("serial")类或包(在 中package-info.java)来清除警告。如果您想强制阻止您的类的实例被序列化,请添加(从内存中):

private static final java.io.ObjectStreamField[] serialPersistentFields = {
    null
};
private void writeObject(
    java.io.ObjectOutputStream ou
) throws java.io.IOException {
    throw new java.io.NotSerializableException();
}
private void readObject(
    java.io.ObjectInputStream in
) throws java.io.IOException, java.lang.ClassNotFoundException {
    throw new java.io.NotSerializableException();
}
private void readObjectNoData(
) throws java.io.ObjectStreamException {
    throw new java.io.NotSerializableException();
}

It's probably not the best thought out system in the world (although it is much better than many people give it credit for).

它可能不是世界上经过深思熟虑的最佳系统(尽管它比许多人认为的要好得多)。

回答by renu

Java Serialization-----Have you ever seen what is inside a serialized object? I will explain you what is java serialization, then provide you with a sample for serialization. Finally most importantly, lets explore what is inside a serialized object and what it means. That is internals of java serialization and how does it works. If you want to have your own implementation of java serialization, this article will provide you with a good platform to launch.

Java Serialization-----你见过序列化对象里面有什么吗?我将向您解释什么是java序列化,然后为您提供序列化示例。最后最重要的是,让我们探索序列化对象的内部内容及其含义。这是java序列化的内部原理以及它是如何工作的。如果你想拥有自己的java序列化实现,本文将为你提供一个很好的启动平台。

What is Java Serialization? Primary purpose of java serialization is to write an object into a stream, so that it can be transported through a network and that object can be rebuilt again. When there are two different parties involved, you need a protocol to rebuild the exact same object again. Java serialization API just provides you that. Other ways you can leverage the feature of serialization is, you can use it to perform a deep copy.

什么是 Java 序列化?java序列化的主要目的是将一个对象写入一个流中,这样它就可以通过网络传输,并且可以再次重建该对象。当涉及两个不同的参与方时,您需要一个协议来再次重建完全相同的对象。Java 序列化 API 只是为您提供了这一点。您可以利用序列化功能的其他方法是,您可以使用它来执行深度复制。

Why I used ‘primary purpose' in the above definition is, sometimes people use java serialization as a replacement for database. Just a placeholder where you can persist an object across sessions. This is not the primary purpose of java serialization. Sometimes, when I interview candidates for Java I hear them saying java serialization is used for storing (to preserve the state) an object and retrieving it. They use it synonymously with database. This is a wrong perception for serialization.

为什么我在上面的定义中使用“主要目的”是,有时人们使用 java 序列化作为数据库的替代品。只是一个占位符,您可以在其中跨会话保留对象。这不是java序列化的主要目的。有时,当我面试 Java 候选人时,我听到他们说 Java 序列化用于存储(以保留状态)对象并检索它。他们将它用作数据库的同义词。这是对序列化的错误认识。

How do you serialize? When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Java API takes care of the serialization protocol and persists the java object in a file in conformance with the protocol. De-serialization is the process of getting the object back from the file to its original form.

你如何序列化?当你想序列化一个对象时,相应的类应该实现可序列化的标记接口。它只是通知编译器这个 java 类可以被序列化。您可以将不应序列化的属性标记为瞬态。您打开一个流并将对象写入其中。Java API 负责序列化协议并将 java 对象保存在符合协议的文件中。反序列化是将对象从文件中恢复到其原始形式的过程。

Here protocol means, understanding between serializing person and de-serializing person. What will be the contents of file containing the serialized object?

这里的协议意味着,序列化人与反序列化人之间的理解。包含序列化对象的文件的内容是什么?