Java Serialization
Java Serialization
Java
Serialization
pg. 1
Java | Serialization
Table of Contents
Introduction ...........................................................................................................................................................................3
Serialization ...........................................................................................................................................................................3
Advantages of Serialization ...................................................................................................................................................3
Steps to serialize an Object ...................................................................................................................................................4
Uses of customizing the serializable mechanism ..................................................................................................................5
Steps to customize the serialization process.........................................................................................................................5
Deserialization .......................................................................................................................................................................6
Advantanges of Serialization .................................................................................................................................................6
Disadvantanges of Serialization.............................................................................................................................................7
Example .................................................................................................................................................................................7
pg. 2
Java | Serialization
Introduction
Serialization is the conversion of the state of an object into a byte stream;
deserialization does the opposite. Stated differently, serialization is the
conversion of a Java object into a static stream (sequence) of bytes, which we can
then save to a database or transfer over a network.
Serialization
• Serialization is a mechanism of converting the state of an object into a
byte stream.
• The byte stream created is platform independent. So, the object serialized
on one platform can be deserialized on a different platform.
• Serialization is mainly used in Hibernate, RMI, JPA, EJB and JMS
technologies.
• If a parent class has implemented Serializable interface then child class
doesn’t need to implement it but vice-versa is not true.
• Only non-static data members are saved via Serialization process.
• Static data members and transient data members are not saved via
Serialization process.
• Associated objects must be implementing Serializable interface.
• The Serialization runtime associates a version number with each
Serializable class called a serialVersionUID, which is used during
Deserialization to verify that sender and receiver of a serialized object
have loaded classes for that object which are compatible with respect to
serialization.
Advantages of Serialization
• Used for marshaling (traveling the state of an object on the network)
• To persist or save an object’s state
• JVM independent
• Easy to understand and customize
pg. 3
Java | Serialization
pg. 4
Java | Serialization
If you want default behavior along with custom behavior then we need to
call defaultWriteObject() available in ObjectOutputStream. When we implement
custom serialization mechanism then we need to implement custom
deserialization mechanism otherwise we won’t get the exact object.
Uses of customizing the serializable mechanism
• We can have more control on the serialization process.
• We can even write static/transient data to stream.
• We can provide extra security on the data like encoding.
Steps to customize the serialization process
1. Create a class and implement the Serializable interface on that class.
2. Provide the custom mechanism for Serialization by implementing
writeObject(ObjectOutputStram) method.
pg. 5
Java | Serialization
Deserialization
• Deserialization is the reverse process where the byte stream is used to
recreate the actual Java object in memory.
• In Deserialization constructor can't invoked.
• We need to have customized deserialization mechanism when
customization serialization mechanism involved.
• To provide custom deserialization mechanism then we need to implement
below method in the serializable class.
Advantanges of Serialization
• Serialization process is a built-in feature that does not require third-party
software to execute Serialization
• The Serialization procedure is proven to be simple and easy to understand
• Serialization procedure is universal and developers from different
background are familiar to it
• It is easy to use and simple to customize
• Serialized data streams support Encryption, Compression, Authentication
and secure Java computing
• There are many critical technologies relying on serialization.
pg. 6
Java | Serialization
Disadvantanges of Serialization
• Objects while Deserialization becomes brittle and they are not sure to be
Deserialized effectively.
• The Transient variables declared while Serialization creates memory space,
but the constructor is not called which results in the failure in the
initialization of transient variables resulting in a variation to the Standard
Java Flow.
• The process of serialization is inefficient in terms of memory utilization.
• Serialization is not preferable to be used in the applications which need
concurrent access without the requirement of third-party APIs, as
Serialization does not offer any transition control mechanism per every SE.
• Serialization procedure fails to offer fine-grained control to access Objects.
Example
pg. 8