Java Full Stack: Methods, Stack Frame & Garbage Collection

🚀 Day 15 – Java Full Stack Journey | Methods, Stack Frame & Garbage Collection Today’s session was a deep dive into one of the most important pillars of Java: 👉 Methods (Functions) 👉 Stack & Heap Memory Behavior 👉 Garbage Collection Not just writing code — but understanding what happens inside RAM during execution. 🔹 1️⃣ What is a Method? A method is simply: A block of code that performs a specific task. Basic structure: public int add() { int c = a + b; return c; } Every method has: Access Modifier (public) Return Type (int) Method Name (add) Parentheses (input parameters) Body (logic) 🔹 2️⃣ Types of Methods (Covered Today) ✅ Method with No Input & No Output public void add() { int c = a + b; System.out.println(c); } Doesn’t take parameters Doesn’t return anything Uses void ✅ Method with No Input but Returns Output public int add() { int c = a + b; return c; } Key difference: print → Displays value on console return → Sends value back to caller Understanding this difference is crucial. 🔹 3️⃣ What Happens in Memory? (Very Important) When a program runs: Java Runtime Environment (JRE) has: Code Segment Stack Segment Heap Segment Static Segment 🧠 Stack Segment Stores method stack frames Stores local variables Follows LIFO (Last In, First Out) When a method is called: A stack frame is pushed After execution, it is popped 🧠 Heap Segment Stores objects Stores instance variables Objects live here If an object has no reference: 👉 It becomes a Garbage Object Java’s Garbage Collector automatically cleans it. No manual memory deallocation needed (unlike C/C++). 🔹 4️⃣ Important Concept: Return Type & Type Casting Return type must match the returned value Implicit type casting works during return Example: int → float ✅ (Implicit) float → int ❌ (Needs explicit casting) Even during return, Java follows type promotion rules. 💡 Biggest Takeaway Today Understanding: Method execution flow Stack frame creation Heap memory allocation Garbage collection Return vs Print difference This is what separates: ❌ Syntax learners From ✅ Real Java developers Today wasn’t about output. It was about internal execution clarity. Day 15 Complete ✔ #Day15 #Java #CoreJava #Methods #JVM #StackAndHeap #GarbageCollection #OOPS #FullStackJourney #LearningInPublic TAP Academy

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories