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

14 Upvotes

15 comments sorted by

View all comments

Show parent comment

2

u/Reasonable-Pay-8771 9d ago edited 9d 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 9d 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.