๐ง What is Stack, Heap Memory, and String Pool in Java?
If you’ve started learning Java, you’ve probably heard people say things like: “That variable is stored on the stack,” or “The object lives in the heap.” And then there’s something called the String Pool — sounds confusing, right? Don’t worry ๐ — this post will make everything super simple to understand. ๐งฉ Let’s start with: What happens when you run a Java program? When your Java code runs, the JVM (Java Virtual Machine) divides memory into different sections . The two most important ones are: Stack Memory Heap Memory Let’s see what each one does ๐ ๐ฅ 1. Stack Memory — for short-term data Think of Stack Memory as a temporary notebook ๐ that the JVM uses to store small, short-lived data. ✳️ Used for: Primitive variables ( int , char , boolean , etc.) References to objects (not the objects themselves) Method calls and local variables Every time a method is called, the JVM creates a stack frame for it. When the method finishes, its frame is r...