In Java, the Serializable
interface is a marker interface that allows a class to be converted into a byte stream, making it possible to save the state of an object or send it over a network. However, there are certain scenarios where you should prefer using Serializable
and others where it might be better to avoid it.
Serializable
when you need to save the state of an object to a file or a database.ArrayList
, HashMap
), and you need to store and retrieve serialized objects.InvalidClassException
.
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private transient int age; // transient field will not be serialized
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and Setters
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?