🌟 Understanding Methods in Java & How Memory Works (Stack vs Heap) 📍Today, I strengthened my understanding of Methods in Java and how memory is managed internally using Stack and Heap. 🔹 What is a Method? A method is a block of code that performs a specific task. It helps in: ✔ Code reusability ✔ Reducing redundancy ✔ Improving readability ✔ Better modular programming 🔹 Different Types of Methods 1️⃣ No Input, No Output 2️⃣ No Input, With Output 3️⃣ With Input, No Output 4️⃣ With Input, With Output ✅ (Most commonly used) 🔹 How Memory Works: Stack vs Heap 🟦 Stack Memory Stores local variables Stores method calls (stack frames) Follows LIFO (Last In, First Out) Automatically cleared after execution 🟩 Heap Memory Stores objects Shared across methods Managed by Garbage Collector When we create an object: Java 👇 Calculator calc = new Calculator(); 👉 calc reference is stored in Stack 👉 Actual object is stored in Heap. 💻 Real-Time Example: Simple Calculator Java 👇 class Calculator { int a = 50; int b = 40; public int add() { int c = a + b; return c; } } public class Demo { public static void main(String[] args) { Calculator calc = new Calculator(); int result = calc.add(); System.out.println(result); // Output: 90 } } 🔎 What Happens Internally? ✔ Object created in Heap ✔ Reference stored in Stack ✔ Method call creates a new stack frame ✔ After execution, stack frame is removed ✔ Heap object remains until garbage collected 🎯 Important Points to Remember ✅ Every method must have a name ✅ Method syntax: accessModifier returnType methodName() ✅ Local variables → Stack ✅ Objects → Heap ✅ After execution, unused objects are removed by Garbage Collection ✅ Methods improve modularity and maintainability ⚫Understanding memory flow helped me clearly visualize how Java executes programs behind the scenes. TAP Academy #Java #CoreJava #LearningJourney #StackAndHeap #Programming #SoftwareDevelopment #InternshipJourney 🚀
Great progress Karthiga Saravanan! Love seeing people invest in learning Java and growing their skills. If you’re looking for structured practice, feel free to check out our free course: https://www.javapro.academy/bootcamp/the-complete-core-java-course-from-basics-to-advanced/ Keep up the awesome work!
Ok, Thank you