Serialization and deserialization of Java

Source: Internet
Author: User
Tags object serialization

The public interface Serializable class enables its serialization functionality by implementing the Java.io.Serializable interface. Classes that do not implement this interface will not be able to serialize or deserialize any of their states. All the subtypes of a serializable class are themselves serializable. The serialized interface has no methods or fields and is used only to identify the semantics of serializable.

Serialization is the preservation of the state of an object (the amount of each property), which is then obtained at the appropriate time.
  
Serialization consists of two parts: serialization and deserialization. Serialization is the first part of this process that decomposes data into a byte stream for storage in a file or on a network. Deserialization is the opening of a byte stream and refactoring the object. Object serialization not only converts the base data type to a byte representation, but sometimes restores the data. Recovering data requires an object instance with recovery data
  
What are the characteristics of serialization:
  
If a class can be serialized, its subclasses can also be serialized.

member data declared as static and transient types cannot be serialized.

Static is shared by the class, and when an object of that class is serialized, the static variable may be changed by another object, so this determines that the static variable cannot be serialized, but if it is final, it is possible because this is a constant.

If you declare an instance variable with transient, its value does not need to be maintained when the object is stored. In other words, a member variable tagged with the transient keyword does not participate in the serialization process.

When to use serialization:

Object serialization enables the implementation of distributed objects. Main applications For example: RMI to use object serialization to run a service on a remote host, just as you would when running an object on a local machine.

Java object serialization preserves not only the data of an object, but also the data of each object referenced by the object. You can write the entire object hierarchy to a stream of bytes that can be saved in a file or passed on a network connection. Object serialization allows you to "deep copy" the object, which is to copy the object itself and the referenced object itself. Serializing an object may get the entire sequence of objects.

Java code 650) this.width=650; "class=" star "src=" Http://injavawetrust.iteye.com/images/icon_star.png "alt=" Favorites Code "style = "border:0px;"/>

  1. import java.io.Serializable;

  2. /**

  3.  * 

  4. * @author Injavawetrust

  5.  *

  6.  */

  7. Public class Person implements serializable{

  8. Private Static Final long serialversionuid = -399858645849256406l;

  9. Private String name;

  10. Private String sex;

  11. Public String GetName () {

  12. return name;

  13. }

  14. Public void setName (String name) {

  15. this. Name = name;

  16. }

  17. Public String Getsex () {

  18. return sex;

  19. }

  20. Public void setsex (String sex) {

  21. this. Sex = sex;

  22. }

  23. }

Java code 650) this.width=650; "class=" star "src=" Http://injavawetrust.iteye.com/images/icon_star.png "alt=" Favorites Code "style = "border:0px;"/> get ""

  1. import java.io.File;

  2. import Java.io.FileInputStream;

  3. import java.io.FileNotFoundException;

  4. import Java.io.FileOutputStream;

  5. import java.io.IOException;

  6. import Java.io.InputStream;

  7. import Java.io.ObjectInputStream;

  8. import Java.io.ObjectOutputStream;

  9. import Java.io.OutputStream;

  10. /**

  11. * Serialization and deserialization

  12. * @author Injavawetrust

  13.  *

  14.  */

  15. Public class Testperson {

  16. Public Static void Main (string[] args) {

  17. Person person = New person ();

  18. Person.setname ("Java");

  19. Person.setsex ("F");

  20. File File = new file ("D:\\person.txt");

  21. Try {

  22. File.createnewfile ();

  23. } catch (IOException e) {

  24. E.printstacktrace ();

  25. }

  26. Try {

  27. //Serialization

  28. OutputStream OS = new fileoutputstream (file);

  29. ObjectOutputStream Oos = new objectoutputstream (OS);

  30. Oos.writeobject (person);

  31. Oos.flush ();

  32. Oos.close ();

  33. Os.close ();

  34. //Deserialization

  35. InputStream is = new fileinputstream (file);

  36. ObjectInputStream ois = new ObjectInputStream (IS);

  37. Person per = (person) ois.readobject ();

  38. System.out.println ("name[" + per.getname () + "]");

  39. System.out.println ("sex[" + per.getsex () + "]");

  40. Ois.close ();

  41. Is.close ();

  42. } catch (FileNotFoundException e) {

  43. E.printstacktrace ();

  44. } catch (IOException e) {

  45. E.printstacktrace ();

  46. } catch (ClassNotFoundException e) {

  47. E.printstacktrace ();

  48. }

  49. }

  50. }


Serialization and deserialization of 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.