Serializable interface in Java

Source: Internet
Author: User
Tags object serialization
Serialization is the process of writing the state of an object into a byte stream. It executes RMI. Rmi allows Java objects on one machine to call Java object methods on different machines, the object can be provided to the remote method as a parameter. The sender serializes the object and sends it to the receiver for deserialization.
Serialized and deserialized relationship charts form a sequence chart containing cyclic references. This is the overall idea of serialization.
The serializable interface is an interface that supports serialization. Only one object that implements it can be stored and replied by the serialization tool. The serializable interface does not define any Members, but only indicates that a tired object can be serialized, if this class can be serialized, all its subclasses can.
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.

Import java. Io .*;

Public class serializationdemo {
Public static void main (string ARGs []) {

// Object serialization
Try {
Myclass object1 = new myclass ("hello",-7, 2.7e10 );
System. Out. println ("object1:" + object1 );
Fileoutputstream Fos = new fileoutputstream ("serial ");
Objectoutputstream OOS = new objectoutputstream (FOS );
Oos. writeobject (object1 );
Oos. Flush ();
Oos. Close ();
}
Catch (exception e ){
System. Out. println ("exception during serialization:" + E );
System. Exit (0 );
}

// Object deserialization
Try {
Myclass object2;
Fileinputstream FCM = new fileinputstream ("serial ");
Objectinputstream OIS = new objectinputstream (FCM );
Object2 = (myclass) Ois. readobject ();
Ois. Close ();
System. Out. println ("object2:" + object2 );
}
Catch (exception e ){
System. Out. println ("exception during deserialization:" + E );
System. Exit (0 );
}
}
}

Class myclass implements serializable {
String S;
Int I;
Double D;
Public myclass (string S, int I, double D ){
This. S = s;
This. I = I;
This. d = D;
}
Public String tostring (){
Return "s =" + S + "; I =" + I + "; D =" + D;
}
}

Program running result: the instance variables of object1 and object2 are the same, and the output is as follows: object1: S = Hello; I =-7; D = 2.7e10
Object2: S = Hello; I =-7; D = 2.7e10

----------------------------------------------------------
Add:
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 over the network (RMI of modemnovel ). This advantage is very big-because it automatically shields the differences in the operating system, the byte order (people who have developed network programming using C under UNIX should know this concept, I can easily make mistakes here. 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. 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. If a new keyword appears in a later Java version to implement this mechanism, for example, persistence, if I use

Persistence (string S = "chinaunix ")

Then the system automatically performs the processing in the program, so Java implements persistence.

Related Article

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.