Please click on any Advertisement, if you like this blog.

Sunday, January 2, 2011

Serializable - Externalizable

What are transient and volatile variable?                                                                                       
1)Transient variable:    Any data that is defined as transient is a persistent data. The transient modifier can be applied to any instance variable to indicate that its contents are not useful outside of the current context and should never be saved. First, fields that are transient do not get serialized, saving both space and time. Use transient fields to avoid serialization.Java also supports the transient modifier, which can be used to eliminate unnecessary data transfers. Fields defined as transient are not transferred when serialization is used, but this is a rather blunt technique that leads to all-or-nothing transfers of fields.                                                                                     
 2)Volatile variable: If you define a variable as volatile, it tells the compiler not to do any optimizations that would remove reads and writes that keep the field in exact synchronization with the local data in the threads.

What is serialization and serializable                                                                         

Serialization: The serialization is a kind of mechanism that makes a class or a bean persistence by having its properties or fields and state information saved and restored to and from storage.Serialization allows you to save and restore the state of an object. Serialization is the process of writing the state of an object to a byte stream. This is useful when you want to save the state of your program to a persistent storage area, such as a file. At a later time, you may restore these objects by using the process of deserialization. Serialization is also needed to implement Remote Method Invocation (RMI). RMI allows a Java object on one machine to invoke a method of a Java object on a different machine. An object may be supplied as an argument to that remote method. The sending machine serializes the object and transmits it. The receiving machine deserializes it. Assume that an object to be serialized has references to other objects, which, in turn, have references to still more objects. This set of objects and the relationships among them form a directed graph. There may also be circular references within this object graph. That is, object X may contain a reference to object Y, and object Y may contain a reference back to object X. Objects may also contain references to themselves. The object serialization and eserialization facilities have been designed to work correctly in these scenarios. If you attempt to serialize an object at the top of an object graph, all of the other referenced objects are recursively located and serialized. Similarly, during the process of deserialization, all of these objects and their references are correctly restored.  

Serializable : Only an object that implements the Serializable interface can be saved and restored by the serialization facilities. The Serializable interface defines no members. It is simply used to indicate that a class may be serialized. If a class is serializable, all of its subclasses are also serializable. Variables that are declared as transient are not saved by the serialization facilities. Also, static variables are not saved.

 

Please click on any advertisement if you like this blog.

How to make a class or a bean serializable 
By implementing either the java.io.Serializable interface, or the java.io.Externalizable interface. As long as one class in a class's inheritance hierarchy implements Serializable or Externalizable, that class is serializable.
What is externalizable?                                                                                                                      The Java facilities for serialization and deserialization have been designed so that muchof the work to save and restore the state of an object occurs automatically. However,there are cases in which the programmer may need to have control over these processes. For example, it may be desirable to use compression or encryption techniques. The Externalizable interface is designed for these situations. The Externalizable interface defines these two methods: void readExternal(ObjectInput inStream)throws IOException, ClassNotFoundException                                                                             
void writeExternal(ObjectOutput outStream) throws IOException   In these methods, inStream is the byte stream from which the object is to be read, and outStream is the byte stream to which the object is to be written.

What is the difference between serializable and externalizable.                                                
 When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject () two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class's serialization process.

Please click on any advertisement if you like this blog.

1 comment:

  1. Good post.One of the main difference between Serializable and Extenalizable is Control over default Serialization process which gives you chance to optimize it. see here for some more difference between Serialization and Externalization

    ReplyDelete

Please provide your precious comments and suggestion