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

3

9

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;
}
Thracian answered 12/11, 2015 at 14:9 Comment(2)
The Student would not be Serializable.Priestcraft
@Priestcraft is correct, all you need it understand what seriablizable means google.com/…Nesto
G
7

Then Student will behave like a normal class i.e 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

Globetrotter answered 12/11, 2015 at 14:12 Comment(0)
M
4

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

Marbling answered 12/11, 2015 at 14:16 Comment(0)
B
0

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

Bundestag answered 1/8, 2017 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.