首页 > 解决方案 > How does overriding of readObject() and writeObject() methods under the serializable interface, find its application in real life development?

问题描述

So, I was studying serialization and I got to know about us having the ability to override the above stated methods.I was wondering,how could I use it? One idea that came to my mind is maybe encrypting and decrypting the data while writing and reading.I am not sure though,if it is how it finds its application. As a developer, how would you use it?

标签: javaandroidserializationdeserializationparcelable

解决方案


因此,在进一步的研究中,我发现了在 Serialization 接口下覆盖readObject()writeObject()方法的应用程序,由于性能开销,它在 Android 开发中具有重要用途。

所以,JAVA中的Serializable接口在内部使用反射来序列化对象。反射导致创建大量对象,因此具有很大的性能开销。但是如果给定的方法被覆盖,以最大限度地减少垃圾收集费用,序列化读取和写入可以显着更快地执行。

例如,Android 允许实现Parcelable接口,该接口试图取消反射。正如预期的那样,它在方法的默认实现中表现得更好。但可以观察到,在覆盖 readObject() 和 writeObject() 方法时它比默认的快得多。

参考:Parcelable vs Serializable


推荐阅读