Can you explain the concept of Java serialization and how it is implemented?
Serialization in Java is the process of converting an object into a byte stream, which can be persisted or transmitted over a network. Deserialization is the reverse process, where a byte stream is converted back into an object. Java provides ObjectInputStream
and ObjectOutputStream
classes for serialization and deserialization.
Example of serialization:
try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("data.ser"))) { MyObject obj = new MyObject(); outputStream.writeObject(obj); } catch (IOException e) { e.printStackTrace(); }