Functions of Java serialization

Source: Internet
Author: User

No 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.

The following is an example of serializable. If myclass does not implement serializable, the program will throw an exception.

 

Java code

[C-sharp: collapse: showcolumns]
+ Expand sourceview plaincopyprint? ··· · 50 ······· · 90 ····· · 140 · 150
  1. Import java. Io .*;
  2. Public class serializationdemo {
  3. Public static void main (string ARGs []) {
  4. // Serialization
  5. Try {
  6. Myclass object1 = new myclass ("hello",-7, 2.7e10 );
  7. System. Out. println ("object1:" + object1 );
  8. Fileoutputstream Fos = new fileoutputstream ("serial ");
  9. Objectoutputstream OOS = new objectoutputstream (FOS );
  10. Oos. writeobject (object1 );
  11. Oos. Flush ();
  12. Oos. Close ();
  13. } Catch (exception e ){
  14. System. Out. println ("exception during serialization:" + E );
  15. System. Exit (0 );
  16. }
  17. // Deserialization
  18. Try {
  19. Myclass object2;
  20. Fileinputstream FCM = new fileinputstream ("serial ");
  21. Objectinputstream OIS = new objectinputstream (FCM );
  22. Object2 = (myclass) Ois. readobject ();
  23. Ois. Close ();
  24. System. Out. println ("object2:" + object2 );
  25. } Catch (exception e ){
  26. System. Out. println ("exception during deserialization:" + E );
  27. System. Exit (0 );
  28. }
  29. }
  30. }

Import Java. io. *; </P> <p> public class serializationdemo {<br/> Public static void main (string ARGs []) {<br/> // serialization <br/> try {<br/> myclass object1 = new myclass ("hello",-7, 2.7e10); <br/> system. out. println ("object1:" + object1); <br/> fileoutputstream Fos = new fileoutputstream ("serial"); <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/> // deserialization <br/> try {<br/> myclass object2; <br/> fileinputstream FCM = new fileinputstream ("serial"); <br/> objectinputstream OIS = new objectinputstream (FCM); <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/>

  1. ------ Myclass. Java
  2. [Java: collapse: showcolumns]
    + Expand sourceview plaincopyprint? ··· · 50 ······· · 90 ····· · 140 · 150
    1. Class myclass implements serializable {
    2. String S;
    3. Int I;
    4. Double D;
    5. Public myclass (string S, int I, double D ){
    6. This. S = s;
    7. This. I = I;
    8. This. d = D;
    9. }
    10. Public String tostring (){
    11. Return "s =" + S + "; I =" + I + "; D =" + D;
    12. }
    13. }

    Class myclass implements serializable {<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/>}</P> <p>

[Java]
View plaincopyprint?
  1. <Span style = "font-size: Medium"> <span style = "font-size: Small">
  2. </Span>

Comments to translent if no serialization property is required
In short, if you perform class transmission in the network environment, you should still implements serializable.

No 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.

The following is an example of serializable. If myclass does not implement serializable, the program will throw an exception.

 

Java code

[C-sharp: collapse: showcolumns]
+ Expand sourceview plaincopyprint? ··· · 50 ······· · 90 ····· · 140 · 150
  1. Import java. Io .*;
  2. Public class serializationdemo {
  3. Public static void main (string ARGs []) {
  4. // Serialization
  5. Try {
  6. Myclass object1 = new myclass ("hello",-7, 2.7e10 );
  7. System. Out. println ("object1:" + object1 );
  8. Fileoutputstream Fos = new fileoutputstream ("serial ");
  9. Objectoutputstream OOS = new objectoutputstream (FOS );
  10. Oos. writeobject (object1 );
  11. Oos. Flush ();
  12. Oos. Close ();
  13. } Catch (exception e ){
  14. System. Out. println ("exception during serialization:" + E );
  15. System. Exit (0 );
  16. }
  17. // Deserialization
  18. Try {
  19. Myclass object2;
  20. Fileinputstream FCM = new fileinputstream ("serial ");
  21. Objectinputstream OIS = new objectinputstream (FCM );
  22. Object2 = (myclass) Ois. readobject ();
  23. Ois. Close ();
  24. System. Out. println ("object2:" + object2 );
  25. } Catch (exception e ){
  26. System. Out. println ("exception during deserialization:" + E );
  27. System. Exit (0 );
  28. }
  29. }
  30. }

Import Java. io. *; </P> <p> public class serializationdemo {<br/> Public static void main (string ARGs []) {<br/> // serialization <br/> try {<br/> myclass object1 = new myclass ("hello",-7, 2.7e10); <br/> system. out. println ("object1:" + object1); <br/> fileoutputstream Fos = new fileoutputstream ("serial"); <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/> // deserialization <br/> try {<br/> myclass object2; <br/> fileinputstream FCM = new fileinputstream ("serial"); <br/> objectinputstream OIS = new objectinputstream (FCM); <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/>

  1. ------ Myclass. Java
  2. [Java: collapse: showcolumns]
    + Expand sourceview plaincopyprint? ··· · 50 ······· · 90 ····· · 140 · 150
    1. Class myclass implements serializable {
    2. String S;
    3. Int I;
    4. Double D;
    5. Public myclass (string S, int I, double D ){
    6. This. S = s;
    7. This. I = I;
    8. This. d = D;
    9. }
    10. Public String tostring (){
    11. Return "s =" + S + "; I =" + I + "; D =" + D;
    12. }
    13. }

    Class myclass implements serializable {<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/>}</P> <p>

[Java]
View plaincopyprint?
  1. <Span style = "font-size: Medium"> <span style = "font-size: Small">
  2. </Span>

Comments to translent if no serialization property is required
In short, if you perform class transmission in the network environment, you should still implements serializable.

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.