简体   繁体   中英

What happens if I don't implement Serializable when using Hashmap

What would happen if I don't include "implements Serializable?"

public class Student implements Serializable {
    private String studentNumber;
    private String firstName;
    private String lastName;
    private ArrayList<Exam> exams;
}

Then Student will behave like a normal class ie You will not able store the state of the Student object anywhere

Go through this : https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html

The Student would not be Serializable, and it will act like a normal class.

Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized - converted into a replica of the original object.

When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized.

More

Say you have some objects in memory in the form of references(Java) and pointers (C++) and you want to transmit these objects over a network or store them into a Disk. How would you do that?

Think of solution and hold it in your mind.

There are 2 ways.

First, create a memory dump and save it to the disk or transmit it over the network. But that would require a lot of changes to the memory dump or the memory dump would need exactly the same addresses in memory to that the memory references are not violated.

The second answer is Serialization, convert the data to a string(format like JSON) and then transmit or save it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM