Posts

Difference Between Minor GC and Major GC in Java

Image
Java uses Generational Garbage Collection where heap memory is divided into different areas. Based on which area is cleaned, Garbage Collection is classified as Minor GC and Major GC . 🔹 Minor GC (Young Generation GC) Minor GC occurs when the Young Generation memory becomes full. 👉 Newly created objects are stored here, and most objects die quickly. Characteristics Cleans Young Generation (Eden + Survivor spaces) Happens frequently Fast execution Short pause time Promotes long-living objects to Old Generation 🔹 Major GC (Old Generation GC) Major GC occurs when the Old Generation memory becomes full. 👉 This area contains long-lived objects. Characteristics Cleans Old (Tenured) Generation Happens less frequently Slower than Minor GC Longer application pause time More CPU usage 🔹 Key Differences 🔹 Simple Flow Objects created → Young Generation Minor GC runs → removes unused objects Surviving objects → moved to Old Generation Old Generation fills → Major GC runs ✅ Promotional C...

What is Reflection API in Java?

Image
The Reflection API in Java allows a program to inspect and manipulate classes, methods, constructors, fields, and objects at runtime , even if their details are not known during compilation. In simple terms, reflection lets Java analyze and modify its own structure while the program is running . 🔹 Why Reflection API is Used To get class information dynamically Access private fields and methods Create objects at runtime Invoke methods dynamically Used heavily in frameworks like Spring and Hibernate 🔹 Important Classes in Reflection Package Reflection API is available in: java.lang.reflect Common classes: Class Method Field Constructor 🔹 Example of Reflection class Student { private String name = "Rahul"; public void display() { System.out.println("Hello Student"); } } public class Test { public static void main(String[] args) throws Exception { Class<?> cls = Class.forName("Student"); Object obj = cls.get...

What is Docker Used for in Java Applications?

Image
Modern Java applications are no longer deployed only on local servers. Today, developers use containerization technologies to make applications portable, scalable, and easy to deploy. One of the most popular tools for this purpose is Docker . Docker helps Java developers package applications along with all required dependencies into lightweight containers that can run consistently across different environments.                                                   What is Docker? Docker is a containerization platform that allows developers to package an application, its libraries, runtime, and configuration into a single unit called a container . This ensures that a Java application works the same way on a developer’s system, testing server, or production environment. Why Docker is Important for Java Applications Java applications often depend on specific versions of ...

What is Dependency Injection in Java?

Image
Dependency Injection (DI) is a design pattern used to achieve loose coupling between classes by providing required objects (dependencies) from outside instead of creating them inside the class. In simple terms, a class does not create its dependencies — they are injected into it by a framework like Spring. 🔹 Why Dependency Injection? Without Dependency Injection, classes become tightly coupled and difficult to maintain or test. Without DI (Tight Coupling): class Engine { } class Car { Engine engine = new Engine(); // object created internally } Here, Car is tightly dependent on Engine . 🔹 With Dependency Injection (Loose Coupling) class Engine { } class Car { Engine engine; Car(Engine engine) { this.engine = engine; } } Now the dependency is provided from outside, making the code flexible and testable. 🔹 Types of Dependency Injection 1️⃣ Constructor Injection – Dependency provided through constructor 2️⃣ Setter Injection – Dependency provided using s...

What is Deadlock in Java?

Image
A deadlock in Java is a situation where two or more threads are permanently blocked , waiting for each other to release resources. Because each thread holds a lock that another thread needs, none of them can continue execution, and the program gets stuck. Deadlocks are one of the most common problems in multithreaded applications. 🔹 How Deadlock Occurs? Deadlock happens when: Thread-1 locks Resource-A and waits for Resource-B. Thread-2 locks Resource-B and waits for Resource-A. Both threads keep waiting forever. 🔹 Deadlock Example class Test { public static void main(String[] args) { final String resource1 = "Java"; final String resource2 = "Thread"; Thread t1 = new Thread(() -> { synchronized(resource1) { System.out.println("Thread 1 locked resource1"); synchronized(resource2) { System.out.println("Thread 1 locked resource2"); } ...

HashMap vs HashTable in Java

Image
In Java Collections Framework, HashMap and HashTable are used to store data in key-value pairs . Although both look similar, they differ in performance, synchronization, and modern usage. Understanding the difference between HashMap and HashTable is very important for students and developers because it is a frequently asked Java interview question. ✅ What is HashMap? A HashMap is a part of the Java Collections Framework introduced in Java 1.2. It stores data using a hashing mechanism and allows one null key and multiple null values. Example: import java.util.HashMap; HashMap<Integer, String> map = new HashMap<>(); map.put(1, "Java"); map.put(2, "Spring"); map.put(null, "Hibernate"); System.out.println(map); Key Features: Not synchronized Faster performance Allows one null key Allows multiple null values Preferred in modern applications ✅ What is HashTable? A HashTable is a legacy class introduced in Java 1.0. It is synchronized, which mak...

What is Constructor Overloading in Java?

Constructor overloading is an important concept in Java that allows a class to have multiple constructors with different parameter lists. It helps initialize objects in different ways depending on the requirement. This concept is commonly asked in Java interviews and widely used in real-world applications. What is a Constructor? A constructor is a special method in Java that: Has the same name as the class Does not have a return type Is automatically called when an object is created Its main purpose is to initialize object values. What is Constructor Overloading? Constructor Overloading occurs when a class contains more than one constructor with the same name but different parameters. 👉 The constructor executed depends on the arguments passed during object creation. Syntax class ClassName { ClassName() { // default constructor } ClassName( int a) { // parameterized constructor } ClassName( int a, String b) { // another constructor...