Serialversionuid, ObjectInputStream and ObjectOutputStream class, Serializable interface, function and usage of SERIALVERSIONUID

Source: Internet
Author: User

Objects read and written by the ObjectInputStream and ObjectOutputStream classes must implement the Serializable interface, and the transient and static type member variables in the object are not read and write

Serializable is actually an empty interface

Package Java.io;public interface Serializable {}

Serializable is an empty interface, there is nothing specific, its purpose is simply to identify a class of objects can be serialized. What happens when serialization is required

A) When you want to write an object in memory to the hard disk;

b) When you want to use sockets to transfer objects on the network;

c) When you want to transfer objects through RMI, and then slightly explain: a) For example, your memory is not enough, the computer will be a part of the memory of the object temporarily saved to the hard disk, and then to use the time to read into memory, the hard disk that part of the storage space is called virtual memory. In the case of you want to save a particular object to a file, I take it out every few days, then the implementation of the Serializable interface;

public static void Main (String args[]) throws Exception {class Student implements Serializable {private static final long Serialversionuid = 0XBC9903F7986DF52FL; String name;int id; int age; String Department;public Student (string name, int id, int age, string department) {This.age = Age;this.department = Depart ment;this.id = Id;this.name = name;}} Student s1=new Student ("Zhang 23", 1, 5, "chemistry"); Student s2=new Student ("Li 41", 2, 9, "creature"); FileOutputStream fout = new FileOutputStream ("C:\\student.txt"); ObjectOutputStream out=new ObjectOutputStream (fout) ; Out.writeobject (S1); Out.writeobject (S2); Out.close (); FileInputStream fin=new FileInputStream ("C:\\student.txt"); ObjectInputStream in=new ObjectInputStream (Fin); try {s1= (Student) In.readobject (); s2= (Student) in.readobject (); catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} In.close (); System.out.print ("Name:" +s1.name); System.out.print ("ID:" +s1.id); System.out.print ("Age:" +s1.age); SYSTEM.OUT.PRINTLN ("Department:" +s1.department); System.out.print ("Name:" +s2.name); System.out.print ("ID:" +s2.id); System.out.print ("Age:" +s2.age); SYSTEM.OUT.PRINTLN ("department:" +s2.department);}

Above is an example of serialization deserialization

Then we'll consider the following questions

Question one: Suppose there is a and B end, if the 2 serialversionuid inconsistent, what error will be generated?

Question two: Assuming that 2 serialversionuid consistent, if the end of a add a field, B end is unchanged, what would it be?

Question three: Suppose 2 serialversionuid consistent, if the B segment adds a field, the end of a does not change, what will be?

Question four: Assuming 2 serialversionuid consistent, if a end of a field, B end is unchanged, what will be the case?

Question five: Assuming that 2 serialversionuid consistent, if the B-side to reduce a field, the end of a is unchanged, what will be the case?

Elicit the question: the function and usage of SERIALVERSIONUID

Serialversionuid, ObjectInputStream and ObjectOutputStream class, Serializable interface, function and usage of SERIALVERSIONUID

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.