Java Note 9: Object serialization and deserialization

Source: Internet
Author: User

During the java program running process, many objects exist at the same time, but these objects disappear when the program ends or the JVM stops running. How can we save these objects so that they can be read into the memory next time? Or how to upload some objects to the java program at the other end through the network? This operation for implementing objects is called object serialization (or persistence), and re-reading the memory is called deserialization.

The packaging class of the basic data type and all container classes can be serialized. Custom classes cannot be serialized by default. If you want to serialize a defined class, you must implement the java. io. Serializable interface for this class.

Here is a Demo:

            Point                     z;        Point( x, y,         .x =         .y =         .z =                 "(" + x + "," + y + "," + z + ")"                       main(String[] args)          String fileName = "F:\\shar\\test\\test7.txt"                  ObjectOutputStream oos =  ObjectOutputStream(          ( i = 0; i < 10; i++             oos.writeObject( Point(i,2*i,3*                     ObjectInputStream ois =  ObjectInputStream(          ( i = 0; i < 10; i++             Point p =             System.out.println(p + " "     }

Running result:
(0, 0)
(1, 2, 0)
(2, 4, 0)
(3, 6, 0)
(4,8, 0)
(5, 10, 0)
(6, 12, 0)
(7, 14, 0)
(8, 16, 0)
(9, 18, 0)

The running results show that the z attributes of all Piont class objects are not serialized. It should have been modified by a keyword transient. The attributes modified by this keyword do not participate in persistence.

 

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.