In Java, serialization is a process of converting an object into a byte stream, which can then be easily stored in a file or sent over a network. The Java platform provides built-in support for serialization, allowing you to convert an object into a sequence of bytes representing its state.
To make a Java object serializable, the class of the object must implement the java.io.Serializable
interface. This marks the class as capable of serialization. Here is a simple example:
import java.io.*;
class Employee implements Serializable {
private String name;
private int id;
public Employee(String name, int id) {
this.name = name;
this.id = id;
}
}
public class Main {
public static void main(String[] args) {
Employee emp = new Employee("John Doe", 123);
try {
// Serialization
FileOutputStream fileOut = new FileOutputStream("employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(emp);
out.close();
fileOut.close();
System.out.println("Serialized data is saved in employee.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
}
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?