This discussion is archived
3 Replies Latest reply: Dec 4, 2013 7:02 AM by vinaykumar2 RSS

Difference between Serialization Vs Externalization

1005975 Newbie
Currently Being Moderated

hi can anyone tell me Difference between Serialization Vs Externalization

  • 1. Re: Difference between Serialization Vs Externalization
    vinaykumar2 Expert
    Currently Being Moderated

    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.

     

    }

    Other class would be

     

    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.


    Read more: http://www.techartifact.com/blogs/2009/06/serialization-vs-externalization.html#ixzz2mWAYCMRz

  • 2. Re: Difference between Serialization Vs Externalization
    1005975 Newbie
    Currently Being Moderated

    thanks for replying.. can you give me some code example

  • 3. Re: Difference between Serialization Vs Externalization
    vinaykumar2 Expert
    Currently Being Moderated

    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();

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points