Serialization in Java 1
Serialization in Java 1
Serialization in Java 1
Kyle Woolcock
ComS 430
4/4/2014
2
Table of Contents
Introduction .................................................................................................................................................. 3
Why Serialize? ............................................................................................................................................... 3
How to Serialize ............................................................................................................................................ 3
Serializable Interface................................................................................................................................. 3
Externalizable Interface ............................................................................................................................ 3
Using These Interfaces to Serialize ........................................................................................................... 5
What Is Not Serialized? ................................................................................................................................. 6
Problems with Serializing .............................................................................................................................. 6
Versioning ................................................................................................................................................. 6
Object References ..................................................................................................................................... 6
Serializing to XML .......................................................................................................................................... 6
Creating Serializable Classes ..................................................................................................................... 7
Using These Classes to Serialize ................................................................................................................ 8
Conclusion ..................................................................................................................................................... 8
Appendix A: Table of Figures ...................................................................................................................... 10
Appendix B: Acknowledgements ................................................................................................................ 10
Works Cited ................................................................................................................................................. 11
3
Introduction
Serialization is the process of converting objects to bytes that can then be used to reconstruct the
original object. This process has many applications including remote procedure calls and allowing for
persistent data. Serialization is a general programming concept, present in many object-oriented
languages. This tutorial will focus on implementations in Java, and how Java handles serialization
behind the scenes, but the general concepts can be applied to many languages. Alternatives to certain
problems serialization solves to exist. For data persistence, often times databases are used where we
just save the information the object stores instead of the object state itself. Java also supports the
ability to serialize to XML which is more human readable, and allows for communication between
programs written at different times and in different languages. To help read the document, it is
important to note that all variable names appear in a different typeset, all class names are bolded, all
exceptions are italicized, all method names are followed by parentheses, and all Java keywords appear
in bold and italics.
Why Serialize?
Serialization allows for a quick and easy way to store data after a program finishes execution. The
serialized data is independent of the Java virtual machine (JVM) that generated it. This means that as
long as a different computer has access to the class files and the serialized data, the object can be
reconstructed just as it originally was. It also allows for remote procedure calls. To call a method on
another machine, often an object is needed argument. Serialization converts an object to a byte stream
that can then be sent over a network and deserialized on the target machine.
How to Serialize
Serializable Interface
Java provides two different ways to allow a class to be serialized. The first is to implement the
Serializable interface. This is just a marker interface, meaning it contains no methods. Java will also
implement a serialVersionUID variable, although it is advised manual assign the variable. It is the
unique identifier Java uses to tell which class it is reconstructing from a byte stream (more on this later).
This is the quickest and easiest way but gives you very little control over how the data is written. Figure
1 shows an implementation of Serializable with an example of a serialVersionUID variable. The
one in the example was auto generated by Java, but set so that further changes to the class does not
affect it. There will be more on this when we talk about versioning in the section on serialization
problems.
Externalizable Interface
Externalizable is an interface that extends Serializable. Unlike Serializable, Externalizable is not a marker
interface. It requires an implementation of the methods readExternal() and writeExternal(). In addition
to these methods, the class must also have a default constructor. This is because when using
readExternal() and writeExternal(), a constructor is actually called for the object and then its variables
are updates. This allows for a faster execution time than Serializable. To implement readExternal() and
writeExternal() manually write the variables of the class to the output stream given. In Figure 2, there is
an implementation of the Externalizable Interface along with the readExternal() and writeExternal().
4
Figure 1
Figure 2
5
Figure 3
6
Object References
Another common problem is object references within the object to be serialized. These are usually
pointers to a location in memory where the object resides. It doesn’t make sense to store these
pointers sense when the data is deserialized, there is no guarantee that that object still exists there.
This means that all the objects the objects to serialize depends on also need to be serialized (or marked
transient). This happens through a process called pointer unswizzling which means we follow all the
pointers the object to serialize, and serialize those objects as well. In Java, this is taken care of
automatically, we just need to check that all the objects in our class are either serializable or marked as
transient.
Serializing to XML
While standard serialization is extremely simple and effective for data storage and sending objects
across a network, sometimes communication across programs or languages is desired. By converting an
object to XML instead of bytes, we can then deserialize it in a wide variety of programs. It also has the
added benefit of making the data human readable. There are many third party libraries that add
support for XML serialization to Java, but Java has adopted one to come with the Java Development Kit
(JDK). Since Java 1.6, Java Architecture for XML Binding has been included in the JDK. It provides a quick
7
and easy way to produce a schema that we can use to convert objects to XML. To do this it uses Java
annotations to mark out the elements. Since this is a basic introduction to XML serialization, only a few
of the basic annotations are given; see Table 1.
Table 1
Figure 4
8
Figure 5
Conclusion
Serialization is a quick and easy way to keep persistant data as well as use remote procedure calls. Java
has very good native support for serialization and a class can be made serializable in a matter of
minutes. When cross program and/or cross language communication is necessary, serialization to XML
is a better option. This allows for the data to be stored in a standardized way that other programs and
languages can understand.
9
Figure 6
10
Figure 1 ......................................................................................................................................................... 4
Figure 2 ......................................................................................................................................................... 4
Figure 3 ......................................................................................................................................................... 5
Figure 4 ......................................................................................................................................................... 7
Figure 5 ......................................................................................................................................................... 8
Figure 6 ......................................................................................................................................................... 9
Appendix B: Acknowledgements
Most examples are adapted from similar examples from Lars Vogel’s articles.
11
Works Cited
Mandliya, Arpit. Java Tutorial for Beginners. 10 March 2013. 31 March 2014.
Paul, Javin. Javarevisited: Top 10 Java Serialization Intervew Questions and Answers. 16 April 2011. 2
April 2014.
Vogel, Lars. Vogel/a: Java Object Serialization - Tutorial. 4 Febuary 2014. 1 April 2014.