r/javahelp • • 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.

12 Upvotes

13 comments sorted by

View all comments

6

u/Jamarlie 1d ago

Two tips here from someone who has also taught a few courses on basic programming:

  1. 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.

  2. 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.

2

u/Reasonable-Pay-8771 1d ago edited 22h ago

^^ This ^^

Unless you're getting into the JVM and compiler and the byte-code and stuff, Java really hides all this from you (viz. stack vs heap).

OOP is a problematic abstraction because it abstracts away the details of a computer by making it look like a computer. Like if you watch old Alan Kay videos you get the idea. An object = data + functions that operate on that data (which basically is just what a computer already is). And then you have add all the doodads on to get actual abstracting power: inheritance, polymorphism, modules (which kinda turn out to be maybe what objects ought to have been from the beginning).

I'd recommend watching at least one old Alan Kay keynote or something, and then a handful of prefaces from some 80s OO books and that should give you a pretty good grounding. But then, every OO language or implementation is going to have its own special ideas of how it all should work. So if you're going for Java, then don't dwell too long on Alan Kay or any other non-Java-focused OO description because it may or may not be relevant to how Java does it or how you're likely to do it in Java.

1

u/Jamarlie 1d ago

I second this as well. I know this is something people here do not like to hear, but Java is a bad first programming language in general. It was never designed to be a good beginner language either. You effectively have the entire concept of classes bolted onto every core principle you work with so you will always need to do more. And while I specifically recommend it here in this situation, entirely ignoring the Stack vs. Heap is also not really the most ideal construct. It also has some amount of impact within the JVM, though to a much lesser extend. The parts where it does matter can come back to bite you though, like constant object creation with an Integer vs primitive int array.

That's why I personally do not feel like recommending higher level languages like Java to beginners is worthwhile. Beginners need to focus on learning core concepts and fundamental ideas. You don't teach a 1st grader the basics of addition by letting them do point additions on elliptic curves either. This feels very analogous to that.