Functions and examples of serializable interfaces in Java
Purpose:
Without implements serializable, you cannot provide remote calls through RMI (including EJB.
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 operating system differences, byte order, 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.
In short, if class transmission is performed in the network environment, it should still be implements serializable.
Example:
Import java. Io. ioexception;
Import java. Io. objectinputstream;
Import java. Io. objectoutputstream;
Import java. Io. serializable;
Import java.net. serversocket;
Import java.net. Socket;
// Define serialization (object)
Class student implements serializable {
Private int SnO;
Private string sname;
Public student (INT SnO, string sname ){
This. Sno = SnO;
This. sname = sname;
}
Public int getsno (){
Return SnO;
}
Public void setsno (INT SnO ){
This. Sno = SnO;
}
Public String getsname (){
Return sname;
}
Public void setsname (string sname ){
This. sname = sname;
}
@ Override
Public String tostring (){
Return "student ID:" + SnO + "; Name:" + sname;
}
}
// Deserialization process of (object)
Class myclient extends thread {
@ Override
Public void run (){
Try {
Socket S = new socket ("localhost", 9999 );
Objectinputstream OIS = new objectinputstream (S. getinputstream ());
Student Stu = (student) Ois. readobject ();
System. Out. println ("the client receives the student object transmitted by the server program>" + Stu );
Ois. Close ();
S. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (classnotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
// (Object) serialization Process
Class myserver extends thread {
@ Override
Public void run (){
Try {
Serversocket Ss = new serversocket (9999 );
Socket S = ss. Accept ();
Objectoutputstream Ops = new objectoutputstream (S. getoutputstream ());
Student Stu = new student (1, "Zhao Benshan ");
Ops. writeobject (Stu );
Ops. Close ();
S. Close ();
SS. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
// Test
Public class testtransfer {
Public static void main (string [] ARGs ){
New myserver (). Start ();
New myclient (). Start ();
}
}
With reference to this example, we will have a good understanding of the usage and function of the serializable interface.