0% found this document useful (0 votes)
14 views

Java Serialization

Serialization is the process of converting an object into a byte stream for storage or transmission. It allows objects to be saved to files and sent over networks. To serialize an object, its class must implement the Serializable interface. The byte stream can then be used to recreate the object through deserialization. Customizing serialization allows more control over the process, such as writing static/transient data or adding security.

Uploaded by

bnavnredy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java Serialization

Serialization is the process of converting an object into a byte stream for storage or transmission. It allows objects to be saved to files and sent over networks. To serialize an object, its class must implement the Serializable interface. The byte stream can then be used to recreate the object through deserialization. Customizing serialization allows more control over the process, such as writing static/transient data or adding security.

Uploaded by

bnavnredy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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

Steps to serialize an Object


1. Create a class and implement the Serializable interface on that class

2. In client applicatin create FileOutputStream and ObjectOutputStream


objects.
3. In client application use writeObject(Object) method from
ObjectOutputStream to write object state into a file.

The above mentioned steps provides default serialization mechanism. In the


default serialization mechanism we do have control only on the declaring the
variables(like declaring the static and transient variables). If we want to customize
the default mechanism then we’ve provide implemention for below method in
serializable class.

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.

4. In client applicatin create FileOutputStream and ObjectOutputStream


objects.
5. In client application use writeObject(Object) method from
ObjectOutputStream to write object state into a file.

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

Fig-1: Serializable class with custom behavior


pg. 7
Java | Serialization

Fig-2: Client application

pg. 8

You might also like