Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
ADVANTAGES AND DISADVANTAGES
HOW DO WE SERIALIZE AN OBJECT?
WHY DO WE NEED SERIALIZATION?
WHAT IS SERIALIZATION?
CONTROVERSIES ON SERIALIZATION
BEST PRACTICES
WHAT IS SERIALIZATION?
www.edureka.co
JVM-1 JVM-2
Byte Stream
Object Object
Byte Stream
WHAT IS SERIALIZATION?
www.edureka.co
www.edureka.co
Serialization in Java is the process of converting the Java code
Object into a Byte Stream, to transfer the Object Code from one
Java Virtual machine to another and recreate it using the process
of Deserialization.
WHY DO WE NEED SERIALIZATION?
www.edureka.co
WHY DO WE NEED SERIALIZATION?
Persistence
Deep Copy
Caching
Cross JVM Synchronization
Communication
www.edureka.co
HOW DO WE SERIALIZE AN OBJECT?
www.edureka.co
www.edureka.co
To Serialize an object, the parent class should implement
either of the following interfaces:
• java.io.Serializable
• java.io.Externalizable
HOW DO WE SERIALIZE AN OBJECT?
ADVANTAGES AND DISADVANTAGES
www.edureka.co
ADVANTAGES
• Serialization process does not require third-party
software.
• The Serialization procedure is simple .
• Serialization procedure is universal .
• It is easy to use and simple to customize.
• Serialized data streams support Encryption,
Compression, Authentication and secure Java
computing.
• Many critical technologies relying on serialization.
www.edureka.co
DISADVANTAGES
• Objects while Deserialization becomes brittle.
• Serialization results in the failure in the initialization of
transient variables resulting in a variation to the
Standard Java Flow.
• Serialization is inefficient in terms of memory
utilization.
• Serialization does not offer any transition control
mechanism per every SE.
• Serialization procedure fails to offer fine-grained
control to access Objects.
www.edureka.co
PRACTICAL EXAMPLES
www.edureka.co
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
SERIALIZATION USING INHERITANCE
CASE - 1
www.edureka.co
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
SERIALIZATION USING INHERITANCE
CASE - 2
www.edureka.co
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
SERIALIZATION USING INHERITANCE
CASE - 3
www.edureka.co
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
SERIALIZATION USING A
STATIC MEMBER
www.edureka.co
EXTERNALIZABLE INTERFACE
www.edureka.co
Implementation
Methods
Process Public No-Arg Constructor
Backward Compatibility
EXTERNALIZABLE INTERFACE
www.edureka.co
www.edureka.co
• While using Externalizable Interface the user
explicitly mentions the objects to be serialized.
• In Serialization Interface, all the objects and variables are
serialized in the run-time.
IMPLEMENTATION
www.edureka.co
• Externalizable interface consists of two methods, namely:
writeExternal()
readExternal()
• Serializable Interface does not include any methods.
METHODS
www.edureka.co
• Externalizable Interface provides customization to the
Serialization process.
• Serialization Interface will provide the default serialization
process.
PROCESS
www.edureka.co
• Externalizable Interface supports Serialization
regardless of the version control.
• Serialization Interface requires the same version of
JVMs on both the ends, but it incorporates automatic
serialization.
BACKWARD COMPATABILITY
www.edureka.co
• Externalization Interface needs Public No-Arg Constructor to
reconstruct the serialized object.
• Serialization Interface uses reflection to reconstruct the
serialized object or class.
PUBLIC NO-ARG CONSTRUCTOR
TRANSIENT KEYWORD
www.edureka.co
www.edureka.co
TRANSIENT KEYWORD
Transient Keyword
www.edureka.co
www.edureka.co
TRANSIENT KEYWORD
Transient Keyword
Transient Keyword is used as a variable modifier at the time of
the Serialization process. Declaring a variable with transient
keyword avoids the variable from being Serialized.
TRANSIENTKEYWORD
SERIAL VERSION UID
www.edureka.co
www.edureka.co
SERIAL VERSION UID
Serializable objects get associated with a unique identification
number provided by the JVM of the host machine. This Unique
ID is called Serial Version UID.
SERIALVERSIONUID
CONTROVERSIES ON SERIALIZATION
www.edureka.co
www.edureka.co
WHAT IS AN INHERITANCE?
Oracle’s Architects intend to remove Serialization from Java as
they consider it as a Horrible Mistake of 1997.
After hectic research, the developers at Oracle found out a few
flaws in the design of Serialization procedure which pose a
threat to the data.
BEST PRACTICES
www.edureka.co
BEST PRACTICES
• It is recommended use javadoc@ serial tag for
denoting Serializable fields.
• The .ser extension is preferred to be used for files
representing serialized objects.
• It is not recommended for any static or transient
fields to undergo default serialization.
• Extendable classes should not be Serialized unless it
is mandatory.
• Inner Classes should be avoided to be involved in
Serialization.
www.edureka.co
www.edureka.co

What is Serialization in Java? | Java Tutorial | Edureka