Member-only story
Mastering Java Serializable Interface with Examples
When and how you should use it
Overview
In Java, Serialization is the conversion of the state of an object into a byte stream; deserialization does the opposite. Stated differently, serialization is the conversion of a Java object into a static stream (sequence) of bytes, which we can then save to a database or transfer over a network.
The Serializable
interface in Java is used whenever you need to serialize an object, which means converting its state to a byte stream, so that it can be saved to a database, sent over a network, or stored in a file, etc. Conversely, this byte stream can be used to recreate the actual Java object in memory when needed. This process is called deserialization.
Here are some specific cases when you might want to use Serializable
:
- Persisting application data: If you want to save the state of your application to disk, so that it can be reloaded later, you might want to use serialization. For example, many desktop applications allow you to save your work to a file, which can be reloaded later. The state of your work can be represented as a serialized object.
- Sending data over network: If you’re sending data to another machine, that data needs to be converted to a byte stream…