Functions of the serializable interface in Java

Source: Internet
Author: User

Original post address: http://www.ideagrace.com/html/doc/2006/06/30/00893.html

Definition of Object serialization:
Object serialization allows you to convert objects that implement the serializable interface into byte sequences, which can be fully stored for future re-generation of the original objects.

Serialization can be performed not only on the local machine, but also on the Network (RMI ). This advantage is very big-because it automatically shields the differences in the operating system, the byte order (people who have developed network programming in unix c should know this concept) and so on. For example, on the window platform, an object is generated and serialized, and then uploaded to a unix machine through the network. Then, the object can be correctly reconstructed on this unix machine.

Object serialization is mainly used to support two main features:
1. Java's RMI (Remote Method Invocation). Rmi allows you to operate on objects on a remote machine like on a local machine. When sending a message to a remote object, you need to use the serializaiton mechanism to send parameters and receive and return straight messages.

2. The status information of Java JavaBeans. Bean is usually configured during design. Bean status information must be stored so that the status information can be restored when the program is running. This also requires the serializaiton mechanism.

II. Sakulagi and rollingpig also talk about persistence.
I think you should talk about persistence in English. But in Java, only lightweight persistence is supported, that is, lightweight persistence, which is implemented through the serialization mechanism.

Persistence refers to the life cycle of an object, which is determined by whether the program is executed or not, even when the program is terminated. It writes a serializable object to the disk (non-RAM memory on the local machine or other machines) and reads the object to the normal RAM memory when the program is re-called.

Why is the Java serialization Mechanism Implemented by lightweight persistence? Because you must explicitly serialize and deserialize the objects in the program, instead of defining an object directly by a keyword, serialization is then processed by the system.

The following is an example of serialization:

Program name: serializationdemo. Java
Program topic: Implement Object serialization and deserialization
Program Description: This program starts from instantiating an object of the myclass class. This object has three instance variables of the string, Int, and double types, which are information to be stored and restored.

Code content

Import Java. io. *; <br/> public class serializable {<br/> Public static void main (string ARGs []) {<br/> // Object serialization <br/> try {<br/> myclass object1 = new myclass ("hello",-7, 2.7e10 ); <br/> system. out. println ("object1:" + object1); <br/> fileoutputstream Fos = new fileoutputstream ("Reyes"); <br/> objectoutputstream OOS = new objectoutputstream (FOS ); <br/> OOS. writeobject (object1); <br/> OOS. flush (); <br/> OOS. close (); <br/>}catch (exception e) {<br/> system. out. println ("exception during serialization:" + E); <br/> system. exit (0); <br/>}< br/> // object deserialization <br/> try {<br/> myclass object2; <br/> fileinputstream FCM = new fileinputstream ("Reyes"); <br/> objectinputstream OIS = new objectinputstream (fiis); <br/> object2 = (myclass) Ois. readobject (); <br/> Ois. close (); <br/> system. out. println ("object2:" + object2); <br/>} catch (exception e) {<br/> system. out. println ("exception during deserialization:" + E); <br/> system. exit (0 ); <br/>}< br/> class myclass implements serializable {<br/>/** <br/> * <br/> */<br/> Private Static final long serial <MCE: script Type = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"> </MCE: SCRIPT> <MCE: script Type = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"> </MCE: SCRIPT> versionuid = 1l; <br/> string S; <br/> int I; <br/> double D; <br/> Public myclass (string S, int I, double D) {<br/> This. S = s; <br/> This. I = I; <br/> This. D = D; <br/>}< br/> Public String tostring () {<br/> return "s =" + S + "; I = "+ I +"; D = "+ D; <br/>}< br/>} 

Program running result: the instance variables of object1 and object2 are the same. The output is as follows: [code: 1: a55efb5f91] object1: S = Hello; I =-7; D = 2.7e10
Object2: S = Hello; I =-7; D = 2.7e1
0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.