Posts

Showing posts with the label Core Java

Serialization, Deserialization and Externalization in Java

Image
In Java interviews, one of the most common topics you’ll encounter is object persistence —how Java objects are saved, transferred, and reconstructed. This is where concepts like Serialization , Deserialization , and Externalization come into play. These aren’t just theoretical ideas; they are crucial in distributed systems, caching, session management, and inter-process communication . Let’s explore these concepts step by step, highlight their differences, and discuss real-world use cases. 1. What is Serialization in Java? Serialization is the process of converting a Java object into a byte stream so that it can be stored in a file, transmitted over a network, or persisted in a database. How it works: The class must implement the Serializable interface (marker interface). Use ObjectOutputStream.writeObject() to serialize an object. Non-transient, non-static fields are serialized; transient fields are skipped. ...

Understanding Marker Interfaces in Java

Image
In Java, a Marker Interface is an interface that does not contain any methods or fields. It serves as a “tag” or “marker” to give special meaning to the classes that implement it. This way, the Java runtime or compiler can identify those classes and provide them with additional capabilities. 🔹 What is a Marker Interface? A marker interface is essentially an empty interface. When a class implements a marker interface, it tells the JVM that the class should be treated in a particular way. // Example of a Marker Interface public interface Serializable { // No methods or fields } public class Student implements Serializable { private int id; private String name; } Here, the Student class implements Serializable . Even though the interface is empty, the JVM knows that objects of Student can be serialized. 🔹 Popular Examples of Marker Interfaces in Java Serializable – Marks classes so their objects...