About author  I am Java/oracle professional.Working on Java/J2EE technologies and i.e Java,J2ee,Oracle ADF,hibernate,J2ee,PL/sql,Apps for 4+ years.I am passionate about learning new technologies.I am sharing my knowledge. Give your views and suggestion on vinay@techartifact.com http://www.linkedin.com/in/vinaykumar2 Read more from this author


Serialization is the process of converting an object into a sequence of bits so that it can be persisted on a storage medium (such as a file, or a memory buffer) or transmitted across a network connection link. This process of serializing an object is also called deflating or marshalling an object.
The serialization mechanism has been added into the Java language for two reasons:
(1) the JavaBeans mechanism uses serialization.
(2) remote method invocation (RMI) allows you to automatically use objects located at another host in the network just like any local objects.
In order to serialize an object, you need the output stream OutputStream, which must be put into the special serialization stream called ObjectOutputStream. After that, you only need to call the method writeObject() to serialize the object and send it to the output stream. . Classes ObjectInputStream and ObjectOutputStream, which respectively implement the ObjectInput and ObjectOutput interfaces, enable entire objects to be read from or written to a stream (possibly a file). To use serialization with files, we initialize ObjectInputStream and ObjectOutputStream objects with stream objects that read from and write to files—objects of classes FileInputStream and FileOutputStream, respectively
Vinayworld class implements serializable interface.

Import java.io.serializable;
Class vinayworld implements Serializable {
Public String vinay_variable;
Private String vinay_add;
}

Other class would be

Public class vinayotherClass  {
Public static void main (String args[])
{
FileOutputStream fos=new FileOutputStream("vinay.txt");
    ObjectOutputStream oos=new ObjectOutputStream(fos);
Vinayworld vw = new vinayworld();
oos.writeobject(vw);
oos.flush();
oos.close();

In this code object of the vinayworld class is serialized into a file name vinay.txt
Serialization is a Marker interface -Marker Interface is used by java runtime engine (JVM) to identify the class for special processing.
Use serialization when you need to add data to the serialization stream that is not an object data member.

Externalization is same as Sterilization except that WriteObject() and ReadObject() method are called by JVM during sterilization an desterilization of object. One thing you can do with Externalization is that you can store extra information into object like STATIC variables and transient variables or you can add more information if you have any business need. One good example is compressing and uncompressing of data to send it through network or converting one format to other like a BMP image to JPEG or GIF format.
Externalization allows you to customize how serialization is done. By implementing externalization you are controlling what gets serialized ( and what doesnot ) as versus default serialization where all non-transient attributes get serialized.
For “fat data” classes with a large number of attributes only a few of which needs to persisted, externalization will help you reduce the size of serialized stream and the time taken to serialize the object. But there will be an overhead involved because the runtime has to call your methods to read/write objects.

Performance issue
1. Further more if you are subclassing your externalizable class you will want to invoke your superclass’s implementation. So this causes overhead while you subclass your externalizable class.
2. methods in externalizable interface are public. So any malicious program can invoke which results into lossing the prior serialized state.

Difference between serialization and externalization: When you serialize an Externalizable object, a default constructor will be called automatically; only after that will the readExternal() method be called.Use the Externalizable interface when you need complete control over your bean’s serialization (for example, when writing and reading a specific file format).

http://www.coderanch.com/t/201401/Performance/java/Serialization-Vs-Externalisation

http://en.wikipedia.org/wiki/Serialization

http://www.builderau.com.au/program/java/soa/Understand-when-to-serialize-v-externalize-objects-in-Java/0,339024620,339274531,00.htm

http://www.roseindia.net/java/java-exception/serializable-exception.shtml

10 Responses to Serialization Vs Externalization


  1. rahul sharmaNo Gravatar
    Jun 25, 2009

    Very nice article.Good for beginners.


  2. anuragNo Gravatar
    Jun 25, 2009

    good post.Can you please tell me ,in performance wise which one is better.


  3. ashwathNo Gravatar
    Jan 07, 2010

    Hey , is there any way to perform the same function as that of externalization without actually implementing the externalization interface ?


  4. Dilip kumarNo Gravatar
    Nov 16, 2010

    good answer……… thank u so much…


  5. SwatiNo Gravatar
    Dec 05, 2010

    I disagree with one point “Externalization is that you can store extra information into object like STATIC variables and transient variables”.
    Its true but even when you override writeObject or readObject method with that you can write transient field as well. So that’s not the difference between impl of serializable and externalizable interface.


  6. Rabindra SinghNo Gravatar
    Jun 09, 2011

    Hi,
    I am highly impressed with this blog in serialization. It provides us a good amount of information about this topic. We can also refer following links for more information about this

    http://jksnu.blogspot.com/2011/01/serializable-externalizable.html


  7. Laurence WhileyNo Gravatar
    Nov 14, 2011

    I enjoy what you guys tend to be up too. This type of clever work and exposure! Keep up the awesome works guys I’ve you guys to blogroll.


  8. Ramandeep SNo Gravatar
    Nov 25, 2011

    Nice.. From here i understood completely.. Thanks


  9. download asphalt 6 apkNo Gravatar
    Feb 02, 2012

    Serialization Vs Externalization | Techartifact I was recommended this web site by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my trouble. You are incredible! Thanks! your article about Serialization Vs Externalization | Techartifact Best Regards Rolf Yoder

Trackbacks/Pingbacks

  1. PimpThisBlog.com

Leave a Reply