Serialization in java 2013

View previous topic View next topic Go down

Serialization in java 2013

Post by Admin on Sun Sep 15, 2013 4:03 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on"><b>Serialization in java</b><br><br> <b>What is serialization?</b><br>Serialization is a process to translate an object in a state/format that can be transferred over a network, stored in a file and the transmitted state/format can be used to get back the original object.</br> An object is converted to bytes and stored in a file. This process is called serialization.</br> Conversion of bytes into a original object is called de-serialization.</br></br> <b>How to serialize an object in java?</b></br>Object need to implement the Serializable interface to make JVM understand that this object need to be serialize and may require opertaions like to store in a file or transferred over a network.</br>This interface is a marker interface and it does not have any methods.</br></br><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657713065266565&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657725999716598&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657739378994418&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657745934599371&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe></br>Since the interface does not contain any method how will JVM understand to serialize this object?</br>To serialize an object, writeObject(object); method of java.io.ObjectOutputStream class is used. </br>To de-serialize an object, readObject() method of java.io.ObjectInputStream class is used.</br></br> writeObject(object) internally checks whether the object that needs to be serialized is an instance of Serializable interface or not. Here JVM applies a check on an object that needs to be serialized as shown below.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">else if (obj instanceof Serializable) {<br /> writeOrdinaryObject(obj, desc, unshared);<br />}</code></pre> <b>SerialVersionUID</b></br>To make an object serialized, one must use serialVersionUID in the object. This field is used to check if the class that is getting de-serialized is compatible with which version of serialized object.</br>Once returned, the class serialVersionUID is compared to the serialVersionUID of the serialized class, and if there is a mismatch, the deserialization fails and InvalidClassException is thrown.</br>This is declared in a class as : private static final long serialVersionUID = 7526472295622776147L;</br></br> If field marked as transient or static then that field gets ignored by JVM and it does not gets serialized.</br>Since static members are associated with the class and they do not belong to the individual objects, they are not serialized. Static field values will be reinitialized to whatever value they are set to when the class is loaded.</br></br><b>For basic example of serialization in java, click <a href="/2013/07/serialization-java-example.html">here</a></b></br><b>Can static be serialized?, check <a href="/2013/07/serialize-static-variable-in-java.html">here</a></b></br></br></div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on"><b>Serialization in java</b><br><br> <b>What is serialization?</b><br>Serialization is a process to translate an object in a state/format that can be transferred over a network, stored in a file and the transmitted state/format can be used to get back the original object.</br> An object is converted to bytes and stored in a file. This process is called serialization.</br> Conversion of bytes into a original object is called de-serialization.</br></br> <b>How to serialize an object in java?</b></br>Object need to implement the Serializable interface to make JVM understand that this object need to be serialize and may require opertaions like to store in a file or transferred over a network.</br>This interface is a marker interface and it does not have any methods.</br></br><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657713065266565&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657725999716598&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657739378994418&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657745934599371&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe></br>Since the interface does not contain any method how will JVM understand to serialize this object?</br>To serialize an object, writeObject(object); method of java.io.ObjectOutputStream class is used. </br>To de-serialize an object, readObject() method of java.io.ObjectInputStream class is used.</br></br> writeObject(object) internally checks whether the object that needs to be serialized is an instance of Serializable interface or not. Here JVM applies a check on an object that needs to be serialized as shown below.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">else if (obj instanceof Serializable) {<br /> writeOrdinaryObject(obj, desc, unshared);<br />}</code></pre> <b>SerialVersionUID</b></br>To make an object serialized, one must use serialVersionUID in the object. This field is used to check if the class that is getting de-serialized is compatible with which version of serialized object.</br>Once returned, the class serialVersionUID is compared to the serialVersionUID of the serialized class, and if there is a mismatch, the deserialization fails and InvalidClassException is thrown.</br>This is declared in a class as : private static final long serialVersionUID = 7526472295622776147L;</br></br> If field marked as transient or static then that field gets ignored by JVM and it does not gets serialized.</br>Since static members are associated with the class and they do not belong to the individual objects, they are not serialized. Static field values will be reinitialized to whatever value they are set to when the class is loaded.</br></br><b>For basic example of serialization in java, click <a href="/2013/07/serialization-java-example.html">here</a></b></br><b>Can static be serialized?, check <a href="/2013/07/serialize-static-variable-in-java.html">here</a></b></br></br></div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum