1

I see that an array reference points at the memory address of array[0]. What does the memory address of an object variable point to? ie Car c1 = new Car("Chevy", "Tahoe");

Thought maybe it points to the first instance variable but that doesnt make sense b/c can have classes w/o instance variables.

5
  • 4
    It points to the object. In this case c1. Commented Apr 21, 2021 at 19:26
  • It points to the object header. Commented Apr 21, 2021 at 21:29
  • @Louis That makes sense Commented Apr 22, 2021 at 23:01
  • It points to the object. Unclear what you're asking. Commented Apr 23, 2021 at 0:46
  • Array reference holds mem addr of first element [0]. What does the mem addr point to for an Object? Louis above said the Object Header, which I presume is part of all Objects in memory and that's what the mem addr points to... Commented Apr 24, 2021 at 15:35

1 Answer 1

1

What does the memory address of an object variable point to?

An object is just a chunk of memory. In a typical implementation of Java, that chunk is part of a larger range of memory known as the heap.

Exactly what makes up the structure of the object, its particular bits and bytes is of no concern to us regular Java programmers. How the structure of an object is laid out in memory is an implementation detail that does not concern us.

ie Car c1 = new Car("Chevy", "Tahoe");

Two steps involved here.

The right side of the assignment = instantiates the object. This means a chunk of memory in the heap is located. The values passed to the constructor are written into member fields in that memory.

The left side of the assignment = takes the location of that newly created object and assigns it to the variable named c1. If c1 is a local variable, it lives in the stack, in a typical implementation of Java.

In Java we never see the actual memory address. In code such as c1.model, we trust the JVM will find the c1 var on the stack, use its internally-maintained memory address to access the particular Car object, then navigate within that object's allocated chunk of memory to retrieve the value of the member field named model.

diagram of a reference variable in the stack leading to an object living in the heap

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Basil. Gained some add'l understanding from this post...
Basil-I'm curious what specific part of the Car object does c1 point to? I see in your drawing its points the Car object on the heap but what is the actual mem addr stored in c1? the above comment said its the memory addr of the object header. Is every constructed object assigned a header which is used to specify the mem address of each object? and what exactly is the object header?
@chappie Regarding what is the actual memory address of an object in a reference, your question in moot. As Java programmers, we never see the memory address. All we care about is knowing that we can rely on a reference variable to take us to the desired object. As for headers and such, I've never cared to look into it. If you care, you can look at the Java Specifications for the JVM and the language. If not described there, then the matter is an implementation detail. Look at source code of the OpenJDK project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.