Skip to main content

Posts

Showing posts with the label DESERIALIZATION

Java : Serialization, Deserialization and Externalization

What is Serialization in Java Object Serialization in Java is a process used to convert Object into a binary format which can be persisted into disk or sent over network to any other running Java virtual machine; the reverse process of creating object from binary stream is called deserialization in Java. Java provides Serialization API for serializing and deserializing object which includes java.io.Serializable, java.io.Externalizable, ObjectInputStream and ObjectOutputStream etc. Java programmers are free to use default Serialization mechanism which Java uses based upon structure of class but they are also free to use there own custom binary format, which is often advised as Serialization best practice, Because serialized binary format becomes part of Class's exported API and it can potentially break Encapsulation in Java provided by private and package-private fields. Serialization is a process of converting an object into a sequence of bytes which can be persisted to a disk or d...

Java : serialVersionUID? What is the use of serialVersionUID?

serialVersionUID? When you serialize an object using Serialization mechanism (by implementing Serializable interface), there is a possibility that you may face versioning issues and because of these versioning issues, you will not be able to deserialize the object. That is not a good thing. But first, what is this versioning issue that is troubling your serialization process? Well, lets say you created a class, instantiated it, and wrote it out to an object stream. That flattened object sits in the file system for some time. Meanwhile, you update the class file, perhaps adding a new field. Now try to read the flattened object. An exception "java.io.InvalidClassException" will be thrown. You don't understand where it went wrong because the changes in the class seem perfectly fine for you. What is serialVersionUID? Before we start discussing about the solution for this problem, lets first see what is actually causing this problem? Why should any change in a serialized clas...