什么是 Java 中的可序列化?

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

What is Serializable in Java?

javaserialization

提问by Richard H

Possible Duplicate:
What does Serializable mean?

可能的重复:可
序列化是什么意思?

I have

我有

class Person implements Serializable {
}

what is the use of that and what will happen if I simply use

那有什么用,如果我简单地使用会发生什么

class Person {
}

回答by Richard H

serializable is a special interface that specifies that class is serialiazable. It's special in that unlike a normal interface it does not define any methods that must be implemented: it is simply marking the class as serializable. For more info see the Java docs.

serializable 是一个特殊的接口,指定该类是可序列化的。它的特殊之处在于与普通接口不同,它没有定义任何必须实现的方法:它只是将类标记为可序列化。有关更多信息,请参阅 Java文档

As to what "serializable" means it simply means converting an instance of a class (an object) into a format where it can be written to disk, or possibly transmitted over a network. You could for example save your object to disk and reload it later, with all the field values and internal state saved. See the wikipedia pagefor more info.

至于“可序列化”的含义,它仅意味着将类(对象)的实例转换为可以写入磁盘或可能通过网络传输的格式。例如,您可以将对象保存到磁盘并稍后重新加载,同时保存所有字段值和内部状态。有关更多信息,请参阅维基百科页面

回答by Michael Borgwardt

If you never serialize an instance of Person, there is no point in declaring implements Serializable. But if you don't and try to serialize an instance, you'll get a NotSerializableException.

如果您从不序列化 的实例Person,则声明implements Serializable. 但是如果你不这样做并尝试序列化一个实例,你会得到一个NotSerializableException.

回答by Deepak

Serilaization ensures data can sent across the network and can be persisted and restored back to its original state using the serialization/de-serialization mechanism.

序列化确保数据可以通过网络发送,并且可以使用序列化/反序列化机制将数据持久化并恢复到其原始状态。

回答by sfussenegger

This is a marker interfaceto declare this class as serializable. You should google for "java serialization"as this topic is sufficiently covered by hundreds of tutorials and articles. You could even start right at Wikipedia. In a nutshell, serialization is about reading and writing whole object graphs from/to streams like files or network sockets.

这是一个标记接口,用于将此类声明为可序列化。您应该在google 上搜索“java 序列化”,因为数百篇教程和文章已经充分涵盖了该主题。您甚至可以直接从 Wikipedia 开始。简而言之,序列化是关于从/向文件或网络套接字等流读取和写入整个对象图。

回答by ashishjmeshram

Serializable is only a marker interface. It is completely empty.It simply allows the serialization mechanism to verify that the class is able to be persisted.

Serializable 只是一个标记接口。它完全是空的。它只是允许序列化机制验证该类是否能够持久化。

Also see following Why Java needs Serializable interface?

另请参阅以下为什么 Java 需要 Serializable 接口?

回答by lindsten

Basically it's a marker interface that says your class can be serialized. See herefor more info.

基本上它是一个标记接口,表示您的类可以被序列化。请参阅此处了解更多信息。

回答by Agusti-N

The J2SE docsays:

J2SE DOC说:

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

类的可序列化由实现 java.io.Serializable 接口的类启用。未实现此接口的类将不会对其任何状态进行序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。

Basically, it's the interface that you have to implement for serialize classes in java.

基本上,它是您必须为 Java 中的序列化类实现的接口。