本文介绍了Java中的Serializable和Externalizable有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和?

推荐答案

要添加到其他答案,通过实现 java.io.Serializable ,您可以获得类的对象的自动序列化功能。不需要实现任何其他逻辑,它只会工作。 Java运行时将使用反射来弄清楚如何编组和解组对象。

To add to the other answers, by implementating java.io.Serializable, you get "automatic" serialization capability for objects of your class. No need to implement any other logic, it'll just work. The Java runtime will use reflection to figure out how to marshal and unmarshal your objects.

在早期版本的Java中,反射非常慢,因此序列化大对象图(例如在客户端 - 服务器RMI应用程序中)是一个性能问题。为了处理这种情况,提供了 java.io.Externalizable 接口,类似于 java.io.Serializable 但是使用自定义编写的机制来执行编组和解组功能(您需要在类上实现 readExternal writeExternal 方法)。这为您提供了解决反射性能瓶颈的方法。

In earlier version of Java, reflection was very slow, and so serializaing large object graphs (e.g. in client-server RMI applications) was a bit of a performance problem. To handle this situation, the java.io.Externalizable interface was provided, which is like java.io.Serializable but with custom-written mechanisms to perform the marshalling and unmarshalling functions (you need to implement readExternal and writeExternal methods on your class). This gives you the means to get around the reflection performance bottleneck.

在Java的最新版本(当然是1.3版本)中,反射的性能比以前好得多,因此这不是问题。我怀疑你很难从现有JVM的 Externalizable 中获得有意义的好处。

In recent versions of Java (1.3 onwards, certainly) the performance of reflection is vastly better than it used to be, and so this is much less of a problem. I suspect you'd be hard-pressed to get a meaningful benefit from Externalizable with a modern JVM.

此外,内置的Java序列化机制并不是唯一的,您可以获得第三方替换,例如JBoss Serialization,这样可以更快,并且是默认的替代品。

Also, the built-in Java serialization mechanism isn't the only one, you can get third-party replacements, such as JBoss Serialization, which is considerably quicker, and is a drop-in replacement for the default.

Externalizable 的一大缺点就是你必须保持这个自己逻辑 - 如果你在课堂上添加,删除或更改某个字段,你必须更改 writeExternal / readExternal 考虑它的方法。

A big downside of Externalizable is that you have to maintain this logic yourself - if you add, remove or change a field in your class, you have to change your writeExternal/readExternal methods to account for it.

总之, Externalizable 是Java 1.1天的遗留物。实际上已经没有必要了。

In summary, Externalizable is a relic of the Java 1.1 days. There's really no need for it any more.

这篇关于Java中的Serializable和Externalizable有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:56