The role of the serializable interface in Java

Source: Internet
Author: User
Tags object serialization

classes that implement the Java.io.Serializable interface are serializable. Classes that do not implement this interface will not be able to serialize or deserialize their any state.
all subclasses of a serialized class are themselves serializable. This serialized interface has no methods and no fields, and is used only to identify the semantics of serialization. Allows the serialization of a subtype of a non-serialized class, the subtype can assume the state responsible for saving and restoring the domain of the parent type's public, protected, and (if accessible) package. As long as the class (extension) has a parameterless constructor, which initializes its state, the subtype can assume the above duties. In this case, it is an error to declare a serializable class. This error will be detected at run time. That is, you can save the object to a byte stream, and then you can recover!
For example: integer implements the serializable, so you can write an integer object with IO to the file, and then can read from the file, such as when you start to write the object's Intvalue () is 5, then read it is also 5. This reflects the role of the ordered class, which is the object used to transfer the class.

The so-called Serializable, is the Java provides the common data saving and reading interface. where to read from and where to save is hidden behind the function parameters. In this way, any type that implements the serializable interface can be saved to a file or sent to another location as a data stream over the network. You can also use pipelines to transfer to other programs in your system. This greatly simplifies the design of the class. As long as the design of a save a read function can solve all the problems mentioned above.
  

the definition of Object serialization:
The object serialization allows you to convert objects that implement the serializable interface into a sequence of bytes that can be fully stored for later re-generation of the original object.

Serialization can not only be done natively, but also via network operation (RMI). This benefit is great----because it automatically masks the differences in the operating system, byte order (people who have developed network programming under UNIX C should know this concept) and so on. For example, an object is generated on the window platform and serialized and then transmitted over a network to a UNIX machine, which can then be properly refactored on this UNIX machine.


The 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, it is necessary to use the Serializaiton mechanism to send parameters and receive returns straight.

2. Java's JavaBeans. The state information for a bean is usually configured at design time. The state information of the bean must be stored so that it can be restored when the program is running. This also requires a serializaiton mechanism.



Two. Sakulagi and Rollingpig say, I'll say it.
I think you're talking about persistence in English. But the Java language now supports only lightweight persistence, which is 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 (a non-RAM memory on a native or other machine) and then reads the object to the usual RAM memory when the program is called again.

Why is the Java serialization mechanism implemented by lightweight persistence? Because you have to explicitly serialize and deserialize objects in the program, instead of directly defining an object by a keyword to be serialized and then handled by the system accordingly.


Here is an example of serialization:

Program Name: Serializationdemo.java
Program topic: Implementing serialization and deserialization of objects
Program Description: The program is started by instantiating an object of the MyClass class, which has three instance variables, type string, int, double, and is the information that you want to store and restore.

Code content


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);


}


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 results: Object1 and Object2 instance variables are the same, the output is as follows:

Object1:s=hello;i=-7;d=2.7e10object2:s=hello;i=-7;d=2.7e10

The role of the serializable interface in Java

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.