Posts

Showing posts with the label Java Basics

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...

JVM: The Foundation of Java Memory Management

Image
Before we dive into memory management in Java, it’s important to understand what the JVM is , how it works , and what happens internally when a Java program runs. This knowledge directly improves your debugging, performance tuning, and overall coding efficiency. What is the Java Virtual Machine (JVM)? The Java Virtual Machine is the engine that runs Java applications. But it doesn’t execute .java files directly. You write Java code → .java The compiler ( javac ) compiles it to bytecode → .class The JVM reads and executes this platform-independent bytecode . This is what enables Java’s famous mantra: Write once, run anywhere. Any device with a JVM — Windows, Linux, macOS — can run the same .class file. Key Components of the JVM The JVM has three major components that work together to run your code: 1. Class Loader Loads .class files into memory. Handles linking, verifying, and initializing classes. Ensures static variables & static blocks run c...