Have you ever seen what is inside a serialized object? I will explain you what is java serialization, then provide you with a sample for serialization. Finally most importantly, lets explore what is inside a serialized object and what it means. That is internals of java serialization and how does it works. If you want to have your own implementation of java serialization, this article will provide you with a good platform to launch.
What is Java Serialization?
Primary purpose of java serialization is to write an object into a stream, so that it can be transported through a network and that object can be rebuilt again. When there are two different parties involved, you need a protocol to rebuild the exact same object again. Java serialization API just provides you that. Other ways you can leverage the feature of serialization is, you can use it to perform a deep copy.
Why I used ‘primary purpose’ in the above definition is, sometimes people use java serialization as a replacement for database. Just a placeholder where you can persist an object across sessions. This is not the primary purpose of java serialization. Sometimes, when I interview candidates for Java I hear them saying java serialization is used for storing (to preserve the state) an object and retrieving it. They use it synonymously with database. This is a wrong perception for serialization.
How do you serialize?
When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Java API takes care of the serialization protocol and persists the java object in a file in conformance with the protocol. De-serialization is the process of getting the object back from the file to its original form.
Here protocol means, understanding between serializing person and de-serializing person. What will be the contents of file containing the serialized object? This serves as a guideline to de-serialize. Have a look at the following sample and how its serialized file looks.
Sample Source Code for Java Serialization
package com.javapapers.sample;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class SerializationBox implements Serializable {
private byte serializableProp = 10;
public byte getSerializableProp() {
return serializableProp;
}
}
public class SerializationSample {
public static void main(String args[]) throws IOException,
FileNotFoundException, ClassNotFoundException {
SerializationBox serialB = new SerializationBox();
serialize("serial.out", serialB);
SerializationBox sb = (SerializationBox) deSerialize("serial.out");
System.out.println(sb.getSerializableProp());
}
public static void serialize(String outFile, Object serializableObject)
throws IOException {
FileOutputStream fos = new FileOutputStream(outFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializableObject);
}
public static Object deSerialize(String serilizedObject)
throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(serilizedObject);
ObjectInputStream ois = new ObjectInputStream(fis);
return ois.readObject();
}
}
Exploring Java Serialization
Look at following image. After serializing ‘SerializationBox’ in the above sample code, I opened the output in a hex editor. You can use Notepad++ and hex plugin to open the serialized file.
Let us look at contents byte by byte and find out what they are. It starts with “ac ed”. It is is called STREAM_MAGIC. It is a magic number (java API guys says) that is written to the stream header. It denotes that is start of serialzed content.
Similarly every character has a meaning. Actually the serialized file is more bulkier than you would expect, as it has a huge header the meta information of the classes involved and finally the content. Object Serialization Stream Protocol have a look at chapter 6.4.2 Terminal Symbols and Constants. It gives you list of symbols and constants used in serialization.
Decrypting Serialized Java Object
In the image, I have underline a unit of information in a separate color for you to easily identify.
ac ed – STREAM_MAGIC – denotes start of serialzed content
00 05 – STREAM_VERSION – serialization version
73 – TC_OBJECT – new Object
72 – TC_CLASSDESC – new Class Descriptor
00 26 – length of the class name
63 6f 6d 2e 6a 61 76 61 70 61 70 65 72 73 2e 73 61 6d 70 6c 65 2e 53 65 72 69 61 6c 69 7a 61 74 69 6f 6e 42 6f 78 – class name
57 fc 83 ca 02 85 f0 18 – SerialVersionUID
02 – this object is serializable
00 01 – count of properties in the serialzed class – one property in our example
42 00 10 – private byte
73 65 72 69 61 6c 69 7a 61 62 6c 65 50 72 6f 70 78 70 – property name – serializableProp in our example
0a – 10 the value – This is the persisted value of the property in our sample





















Thanks for the insight on serialization
ken on December 4th, 2009 11:37 pmSerialization Concept explained nicely. Thanks for Details.
Prafulla on December 5th, 2009 1:31 amSimple and easy-to-understand explanation. I liked the use of a broad use tool like notepad++ and the Constant mapping. By the way, in wich class this constans are defined?
Omar on December 8th, 2009 9:59 pmExcellent technical stuff, i ever came across on Blogs. Looking forward for new subjects
Ganesh Bhosale on December 9th, 2009 7:15 amOmar, have a look at java API:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectStreamConstants.html
it contains java serialization related constants.
Joe on December 17th, 2009 9:56 amThanks so much for the good explanation on serialization
Suresh on December 17th, 2009 9:52 amThanks for such a precise and clear understanding.
Shadab on December 29th, 2009 7:32 amVery neatly explained…!
Arpit on December 29th, 2009 2:58 pm[...] Also know some fundamentals of serialization. [...]
Serialize / De-Serialize &hellip on January 9th, 2010 2:26 pmToday only I visited this site for the first time and wow..I am in love with its content….very informative indeed. Well its now in my fav list and I have planned to read one topic per day.
Thanks a lot for creating such a useful blog!!!
Sandeep Kumar on January 12th, 2010 4:27 pmHi there,
Nice Explanation in simple and precise format.
keep writing :) .
Cheers
Nitin on February 6th, 2010 9:20 amNitin.V
You made serialization concept just so simple and understandable.
Thanks and keep such articles coming up!
Neha J on February 10th, 2010 10:07 pmNice and easily understandable explanation for java serialization. I have never came across this before.
Srilatha,K on March 31st, 2010 4:43 pmGood Insight
Jill on April 9th, 2010 11:41 amVery Good Explanation. Its very understandable.Thanks
Sridhar on April 13th, 2010 5:02 pmNice understanding explanation..
Thanks a lot
Brijesh on June 17th, 2010 4:13 amHi
Thanks the detail explanation.
Regards,
Suwarna on June 24th, 2010 3:09 pmSuwarna
Hi,
It is very good material on serialization. But i have one doubt.
In EJB we have ejbActivate() and ejbPassivate() methods. In CMP beans the container will serialize the object in ejbActivate() and deserialize in ejbPassivate() methods. Here we are not tranporting the objects via streams to other PCs using serialization protocol. Please explain me in the context of EJB serialization and deserialization?
Thanks
Anand Garlapati.
Anand Garlapati on July 27th, 2010 12:34 pmThanks for giving me this type of information.
divya on September 15th, 2010 4:18 amThis is a very nice and easy to understande.
Good information.
Usman on September 22nd, 2010 7:27 pmThanks
You can get more details here
http://www.javaworld.com/community/node/291
Akhilesh Balakrishnan on November 22nd, 2010 9:20 am5
Hi,
Just to add while writing Serializable class , its good practice to put a Seriazlizable alert in file so that when some one else add a new field in the class he must ensure that field is either transient or Serializable , so that Serializability of class should not break.
Thanks
Javin @ Tibco RV Tutorial on January 28th, 2011 4:18 pmJavin
Thanks 4 clearing my serialization concept.
Shampa on March 7th, 2011 6:58 pmvery cool
rohan on March 9th, 2011 8:10 amSee this
sdfsd on April 5th, 2011 6:38 am[...] serialization, single instance contract of the singleton pattern can be violated. You can serialize and [...]
Singleton Pattern&hellip on May 2nd, 2011 12:46 amExplained very nicely and each line has clear message………….
Prahlad Gupta on May 5th, 2011 5:40 amNice to see detail description,it helps alot to understand internal details.
Please keep posting such a nice blogs.
-Vidhi
vidhi on May 5th, 2011 10:26 am[...] 使用序列化,单例模式与单例模式之间存在冲突。你可以序列化然后反序列化来获取新的相同的单例模式的类的实例。使用Java API,你可以实现如下的方法并从流中读取来覆盖实例。你可以确保总是有一个唯一的实例。 [...]
单例模式 | Java视点&hellip on May 16th, 2011 9:58 amReally good knowledge i got on java serialization
Thanks
Anonymous on May 18th, 2011 2:21 pmPrasanna
Explained in a very Simple Manner.
Akshata on June 22nd, 2011 4:55 amThank You.
Great Blog!!!!!Nice job Done Joe:)
Anji on June 23rd, 2011 8:07 amIts very nice artice , but please cover the Serialization UID concept indetail.
Madhukar Gunda on July 6th, 2011 11:45 amgood knowledge i got on java serialization, but please trasiant varible concept indetails.
anji on July 8th, 2011 8:00 amWOW…..SUPERB GOOD JOB DUDE…Continue with super stuff
sridip on July 11th, 2011 5:17 pmCan i serialise any type of data,means i want to serialise print preview.is it possible???????
swathi on July 12th, 2011 8:46 amThe explanation is very simple and clear. Thanks.
sha on July 19th, 2011 11:54 amvery nice stuffs :)
Anonymous on July 23rd, 2011 3:49 pmbeautiful way of presenting :)
Thank you very much :)
Sandeep
Great Thanks Joseph :-)
Bharath narla on July 28th, 2011 4:50 pmIts very nice artice , but please cover the Serialization whole concept in detail
Jitendra Gupta on August 9th, 2011 5:37 amNice Article. Explained in simple manner.
Mayank Modi on August 22nd, 2011 7:25 amCan you explain the Serialization using Externalizable interface?
thanks in advance.
Gourav on August 23rd, 2011 4:51 amSerialization Concept has been explained in a simple but easily understandable manner….
Jagadish.K on August 24th, 2011 9:16 amIts really cool…
Thank u for ur answer….!:-)
Sakthi Manoj on August 26th, 2011 1:04 pmVery Knowledge full about Serialization
Ashok Kumar on September 14th, 2011 6:24 pmnice one,it is very easy to understand…
govardhan on September 16th, 2011 6:06 amThanks
Very nicely explained.
Good blog.
Thanks
Neha T on September 16th, 2011 8:55 amNeha T
very nice… at last found a good website 4 java learning
sonal on September 26th, 2011 2:57 pmcan we implement Serialization on images…
sonal on September 26th, 2011 2:58 pmbasically i want image to be converted in binary form..
pls reply
easy way to learn from this site……?
Ashish jain on September 29th, 2011 8:15 amvery nice discription
Ashish Gokhale on October 1st, 2011 8:37 amthamks alit!!!!!!!!!!!!!!!!!
Thanks for providing such tough topic in an easiest way…..
Nagraj on October 3rd, 2011 1:16 pmvery helpful, thanks.
wudeng on October 10th, 2011 3:00 amHi
Eman Zaman on October 18th, 2011 5:25 amThanks for such a great post….
Very nice. Above contents are helpful to all.
Thanks for such a great work.
Could you please add more examples like serializing images or Resultset,It will be more helpful
muthu on October 20th, 2011 7:39 ami could not understand
jnana on October 21st, 2011 9:25 am“serialization”
something good in this………
sathish on October 27th, 2011 1:25 pmExcellent creativity.
Basavaraj on November 1st, 2011 1:46 pmSIR AWESOME CODE AND EXCELLENT AND STUDENTS ARE EXACTLY IMPROOVING THERE KNOWLEDGE AUTOMATICALLY BY READ THIS CONCEPT
THANKING YOU SIR ,
RAJ on November 6th, 2011 4:51 pmHi this is good
santhosh on November 9th, 2011 9:24 amthanks to this site develpers
sumit on November 9th, 2011 12:12 pmCan you give some explanation about SerialVersionUID in serialization
John on November 11th, 2011 11:18 amI appreciated it.It is very nice explanation.
sunil kumar chaurasia on November 14th, 2011 9:11 amsir I want to some easy example of file handling,java bean,and networking.I need must.
sunil kumar chaurasia on November 14th, 2011 9:13 amthank u sir
it is a good material for serialization….
easily understandable
Ramkrishna on November 28th, 2011 11:01 amThnks!!!
azaz ahmed on November 28th, 2011 12:27 pmthis is very good nodes
ASHOK CHOWDARY on November 29th, 2011 8:57 amThanks a lot .I came to know a lot about SERIALIZATION .
Anonymous on December 6th, 2011 10:39 am[...] Construction in Serialization 11/12/2011My previous article on exploring java serialization is a box office hit. In continuation to that and popular request, I am going to write on how [...]
Object Construction in Se&hellip on December 11th, 2011 6:26 amNice job……….
Guna on December 13th, 2011 2:30 pmThank a lot….
Kuddos to ur explanation
Praveen on December 16th, 2011 11:28 amawesome yaar………
got to know something new and real helpful
and easy to unerstand..
thnx…
keep it up…..
karan on December 17th, 2011 1:17 pmnice…
biswajit on December 18th, 2011 6:01 pm[...] have seen enough about using the default protocol to implement serialization and how instances are created during serialization. In this current article we shall see about [...]
Customize Default Seriali&hellip on December 18th, 2011 11:49 pmPlease tell me the difference between serialization, transcient and clonable?.
Dinesh on December 19th, 2011 12:23 pmThis is awesome. I am very much clear about serialization
muthu on January 1st, 2012 11:30 amNicely Explained. Good work dude….
rajeshwar on January 4th, 2012 1:56 pmKeep Posting different Topics On java.
thx
Really nice explanation……….
Swaminathan on January 6th, 2012 7:59 amhey really nice blog… keep it up
abhijit on January 10th, 2012 1:03 pmvery good explanation
thank u….
please tell how to serialize object in xml
mazhar on January 11th, 2012 11:07 amthanks to make us understand.
mazhar on January 11th, 2012 11:12 amplease tell how we use xml in serialization.
and how we convert object to xml form.
Excellent Material.
Harish on January 13th, 2012 10:37 am[...] is an interface that enables you to define custom rules and your own mechanism for serialization. Serializable defines standard protocol and provides out of the box serialization [...]
Externalizable vs Seriali&hellip on January 16th, 2012 5:07 amHi Joe , can you explain why strings are immutable in java???
rahul on January 17th, 2012 3:49 pmawesome Tutorial for Serialization
SuresHK on January 17th, 2012 6:16 pm@rahul have a look at java string
Joe on January 17th, 2012 8:57 pmVery neat and clear explanation which never read.
Anonymous on January 22nd, 2012 4:21 pmits very nice def….
Sunil Kumar on January 24th, 2012 10:24 pmHi,
This is very helpful example….
Thanks
Sunil Kumar Maghar on January 24th, 2012 10:29 pmSunil Kumar
Good
prakash on January 25th, 2012 11:53 amNice way of presentation.
I have a question regarding de-serialization.
First you serialize a object SerializationBox and write it to a file outfile.ser. On a different JVM i take the outfile.ser file and try to deserialize it using readObject() but i get java.lang.ClassNotFoundException as there is no class file sent.
How does it reconstruct the object and type cast to the SerializationBox object?
I know the file contains Class information as shown in the hex-format. but how do you write code to use readObject and typecast to the serialized object.
SerializationBox reconstructedSBX = SerializationBox ois.readObject();
Ganesh Machkure on January 27th, 2012 9:03 pmtoooooooooooooo good
Anonymous on February 6th, 2012 2:21 pmExcellent work Joe!!
Anonymous on February 6th, 2012 4:32 pm- Shyam :)