I found an exellent article that explains very well how Java works thanks to the JVM (Java Virtual Machine). If you want understand how Java runs on a CPU, check out this link:
An Illustration of JVM and the Java Program Operation Principle
The highlighted part of the article below is how Java manages data; like the difference between heaps and stacks, where the first one is about how Java stores the objects and the second one is how Java stores variables and others.
Java Tutorial - Learn Java Programming - GeeksforGeeks
Understanding the Java Object Lifecycle
The previous article shows how the 4 phases of an object’s lifecycle work and how the Garbage collector plays a crucial role in the object’s destruction.