You need to understand the slightly lower levels of Java memory organization. Primitives (int, double, boolean, etc.) and links to objects pointing to the heap are saved on the stack.
Inside any object, the same is true. It either contains links to other objects or primitives directly. Objects are always links in any context, and these links are passed by value.
Thus, we can have:
[ STACK ] [ HEAP ] int a: 10; -> MyWrapperObject@21f03b70 ====|| double b: 10.4; | || int someField: 11 || MyWrapperObject@21f03b70 ------| || String@10112222 ---------- ...... ||==========================|| | | | String@10112222 ============||<---- || ... || || ... || }}=========================||
Note that the use in some cases (for example, inside internal JVM objects) of objects can be stored in memory without a heap.
source share