Java Learning--java Serializable

Source: Internet
Author: User

1. What is the serializable interface?

http://en.wikipedia.org/wiki/serialization
The serialization of a class is implemented through the class implementation of the Java.io.Serializable interface. Classes that do not implement this interface will not serialize or deserialize the two states. A subclass of a serialized class (subtype) They are also serialized
。 The serialized interface does not have a method or value, and it is implemented simply to denote the meaning of serialization.

2. Why should there be serialization?

StackOverflow, there are related issues: http: // Stackoverflow.com/questions/2232759/what-is-the-purpose-of-serialization-in-java http: // Stackoverflow.com/questions/441196/why-java-needs-serializable-interface
1. All objects are in memory while the program is running. When the run ends, the memory occupied by the object is reclaimed by the operating system. The program is like forgetting everything that happens when it runs. Serialization just solves this problem,
By saving the object to disk, it can be re-read at the next start. The Java platform allows us to create reusable Java objects in memory, but in general, these objects can exist only when the JVM is running, that is, they will not have a longer life cycle than the JVM.
In a real-world application, however, it is possible to require that the specified object be saved (persisted) after the JVM has stopped running, and that the saved object will be re-read in the future. The serialization of Java objects can help us implement this functionality.
2. Convenient for transport and persistence: serialization is the conversion of an object into a format that can be stored (such as a file or memory cache or transmitted over a network connection). This mechanism allows you to propagate objects over the network and to persist objects at any time
To the database, files and other systems.

3. Since there is no method in serializable, why not all classes are serialized by default?

Why not all classes are serialized by default, and the Java serialization mechanism must be implemented by implementing the Serializable interface. There are three main reasons:1. Not all objects can find useful semantics in the serialized state. For example, a thread object is related to the state of the current JVM. The deserialized thread object does not have a contextual context that preserves useful semantics (context)2. The serialization state of an object forms a class-compatible contract. Ensuring compatibility of different versions of serialization classes requires additional work and consideration. Therefore, the decision to turn a class into a serializable class requires thoughtful design,
Instead of the default situation.
3. Serialization allows access to members of non-ephemeral, private classes that are inherently inaccessible, and classes that contain sensitive information (such as password password) should not be serialized or externalizable.
Importjava.io.Serializable;/*** Class person implementing the Serializable interface *@authorLenovo **/ Public classPersonImplementsserializable{PrivateString name; Private intAge ; Private transient intNo//temporary, this property is not serialized         Public intGetno () {returnNo; }     Public voidSetno (intNO) {         This. No =No; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; } @Override PublicString toString () {return"Name:" + This. name+ "\nage:" + This. age+ "\NSSN:" + This. No; }}
1 ImportJava.io.FileInputStream;2 Importjava.io.FileNotFoundException;3 ImportJava.io.FileOutputStream;4 Importjava.io.IOException;5 ImportJava.io.ObjectInputStream;6 ImportJava.io.ObjectOutputStream;7 8 9  Public classTest {Ten     Public Static voidMain (string[] args) { Oneperson P1 =NewPerson (); AP1.setage (20); -P1.setname ("John"); -P1.setno (123456); the      -     Try { -FileOutputStream FileOutputStream =NewFileOutputStream ("C:\\users\\lenovo\\desktop\\persondemo.txt"); -ObjectOutputStream ObjectOutputStream =NewObjectOutputStream (fileoutputstream); +System.out.println ("1. Serialization:" +p1.getname ()); - System.out.println (p1.tostring ()); + Objectoutputstream.writeobject (p1); A objectoutputstream.close (); at}Catch(FileNotFoundException e) { - e.printstacktrace (); -}Catch(IOException e) { -         //TODO auto-generated Catch block - e.printstacktrace (); -     } in      -     Try{ toFileInputStream FileInputStream =NewFileInputStream ("C:\\users\\lenovo\\desktop\\persondemo.txt"); +ObjectInputStream ObjectInputStream =NewObjectInputStream (fileinputstream); -person P2 =(person) objectinputstream.readobject (); theSystem.out.println ("2. Deserialization:" +p2.getname ()); * System.out.println (p2.tostring ()); $ objectinputstream.close ();Panax Notoginseng}Catch(Exception e) { - e.printstacktrace (); the     } +      A } the}

Operation Result:

Serialized file (saved in 16):

Java Learning--java 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.