Harsha Laxman Harsha Laxman - 4 months ago 15x
Java Question

Basic Memory Model Diagram for Node Class (Java)

I have a question to draw the memory model diagram for the code below:

With the given answer here:

My question is why is the value for next stored inside the box while the values for data (11 and 5) are pointed to outside the box. I am thinking it is because it is a null value, but what is the reason if so?


5 and 11 are primitive values. Since data is a reference type to E, data cannot be a primitive type, so it is a reference (hmm, I already said that) to the object (e.g. Integer) holding the primitive value (e.g. int).

Reference types like E and Node (bad, bad code, using a raw generic), can either reference an object, or be null. Hence null is that actual value of the type.

Similarly, data can reference an object of type E, or be null if your list allow null values.