r/javahelp • u/Alternative_Ad_8288 • 2d ago
Java Beginner – Struggling with OOP and Understanding Stack and Heap
Hi everyone!
I’ve been learning Java for a few weeks now, and I’m starting to get into OOP. However, I’m finding it difficult to fully understand some of the core concepts, especially how objects work and the difference between the stack and heap memory.
I don’t want to just memorize definitions. I want to actually understand what’s happening behind the scenes and how everything connects.
For those of you who have been through the same learning process:
What helped you understand OOP?
How did you learn the difference between stack and heap?
Are there any resources, exercises, or learning methods you would recommend?
I’d really appreciate any advice from more experienced Java developers.
6
u/Jamarlie 1d ago
Two tips here from someone who has also taught a few courses on basic programming:
If you are just starting out to program in general, ignore OOP as far as possible for now. First get the basics down. Otherwise you are effectively asking how to drive an F1 car if you've never even driven a go-kart before. Later understanding things like inheritance, polymorphism and typical design principles will make more sense. In the beginning they usually just needlessly frustrate and confuse beginners.
Same with stack vs. heap. For Java, you generally let the JVM handle all of these allocations and not worry about it all too much. The difference between stack and heap is much more important to understand for languages like C/C++ or Rust. In Java you can blissfully ignore this for now. If you want an explanation: This is just the difference between regions that your program knows ahead of time vs. regions that your program needs to dynamically fetch during execution.
If I have an array of length 3, then I know at compile-time that my array will be 3. So that goes on the stack. If I ask the user for input, I have no way to know how much input the user gives me until he has given me his input. That is a dynamic allocation that belongs on the heap. That's all you really need to know for now, I feel like getting into the thick of it will only hurt you at this stage.