What is the serializable interface in Java for?

Source: Internet
Author: User
Tags serialization serialization definition
Serialization is the process of writing the state of an object to a byte stream, it executes Rmi,rmi allows Java objects on one machine to invoke Java object methods on different machines, and objects can be supplied as parameters to that remote method, the transmitter serializes the object and transmits it, and the receiver performs deserialization.
A diagram of the relationship between serialization and deserialization can form a sequential chart that contains circular references. This is the overall idea of serialization.
The serializable interface belongs to an interface that supports serialization, and only one object that implements it can be stored and replied to by the serialization tool, and the serializable interface does not define any members, only to indicate that a tired can be serialized, and if the class can be serialized, then all its subclasses can.
Here is an example of serialization:
Program Name: Serializationdemo.java
Program theme: Implementing serialization and deserialization of objects
Program Description: The program starts with an object that instantiates an MyClass class that has three instance variables, string, int, double, and is the information you want to store and recover.



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 fis=new fileinputstream ("Serial");

	Objectinputstream ois=new objectinputstream (FIS);

	Object2= (MyClass) ois.readobject ();

	Ois.close ();

	System.out.println ("Object2:" +object2); The 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 run Result: Object1 and Object2 instance variables are the same, the output is as follows:
Object1:s=hello;i=-7;d=2.7e10

Object2:s=hello;i=-7;d=2.7e10
Add: Object serialization definition: object serialization  allows you to convert an object that implements the serializable interface into a byte sequence. These byte sequences can be stored completely to regenerate the original objects later.   Serialization can be done in this machine and can be operated via network (that is, the RMI of cat fiction). This benefit is very large----because it automatically masks the operating system differences, byte order (with UNIX under the C Development of network programming people should know this concept, I will easily make mistakes on this) and so on. 


For example, generate an object on the window platform and serialize it, then upload it over the network to a UNIX machine, and then refactor the object correctly on the UNIX machine. Object serialization is mainly used to support 2 main features: 1. Java RMI (remote method invocation). RMI allows you to manipulate objects on a remote machine as you would on this computer. 

When sending a message to a remote object, you need to use the Serializaiton mechanism to send the parameters and receive the return straight. 2. The state information for Java Javabeans.   bean is usually configured at design time. The state information of the bean must be saved so that the state information can be recovered when the program is running. 



This also requires a serializaiton mechanism. Two. 
Sakulagi and Rollingpig said the permanence I also said. 

I think you should be talking about persistence in English. But the Java language now supports only lightweight persistence, lightweight persistence, which is achieved through the serialization mechanism. Persistence refers to the life cycle of an object that is not determined by the execution of the program, even if the object exists at the end of the program. 

It writes a serializable object to a disk (not RAM memory on this machine or another machine) and then reads the object to the usual RAM memory when the program is called back. Why does the Java serialization mechanism implement Lightweight persistence? Because you have to explicitly serialize and deserialize the object in the program Rather than directly by a keyword to define an object to be serialized and then handled by the system accordingly.    If a later Java version appears with a newKeyword to implement this mechanism, such as 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.