Aditya Kumar’s Post

Stack vs Heap Memory in Java – Where Does Your Data Live? 🧠 Stack Memory: ▸ Method calls + local variables   ▸ LIFO (Last In, First Out)   ▸ Very fast, auto-cleared after method ends   ▸ Each thread has its own stack  Heap Memory: ▸ Stores objects & instance variables   ▸ Shared across threads   ▸ Managed by Garbage Collector   ▸ Slower than Stack, can throw OutOfMemoryError  Example: void demo() {   int x = 10;   String s = new String("Java"); } ▸ x → Stack   ▸ s (reference) → Stack   ▸ "Java" object → Heap  Rule: → Primitives → Stack   → References → Stack   → Objects → Heap  #Java #SpringBoot #BackendDevelopment #Memory #JavaDeveloper

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories