What is serialization, and what does the Serializable interface do?
Must Know🌱 FresherSerialization converts an object's state into a byte stream (so it can be saved to disk, sent over a network, or stored in a cache), and deserialization reconstructs an equivalent object from those bytes. A class opts in simply by implementing the marker interface Serializable — it declares no methods; it just signals to ObjectOutputStream/ObjectInputStream that this class is allowed to be serialized.
try (var out = new ObjectOutputStream(new FileOutputStream("user.ser"))) {
out.writeObject(user);
}
try (var in = new ObjectInputStream(new FileInputStream("user.ser"))) {
User restored = (User) in.readObject();
}