How does Java's reference variable stored? Is that work similar to C pointer?
what I mean by reference variable is myDog in this code
Dog myDog = new Dog();
I understood about C pointer, it stores in the heap if global variable, and it stores in the stack if local variable. I wonder java works same way.
Reference is one type of variable stored on stack because of there is need to perform operation on them where object stored on heap. In c++ references are stored in stack surely. When we talk about reference in java, it places Heap area.
A reference variable is a variable that points to an object of a given class, letting you access the value of an object. An object is a compound data structure that holds values that you can manipulate. A reference variable does not store its own values.
If r is a variable, the address of r is &r. It's not a reference, C is not C++. So if r = &a, &r represents the address where the address of a is stocked. Where does the reference variable gets stored This is unspecified.
Reference variable is used to point object/values. 2. Classes, interfaces, arrays, enumerations, and, annotations are reference types in Java. Reference variables hold the objects/values of reference types in Java.
All the references to s1, s2, ob1, obj2 and obj3 will be stored in the Stack.
The objects data, will be stored in the Heap (and for String for example in can be stored in a special constant pool).
You need to understand a bit of the lower levels of Java memory organization. On the stack, primitives(int, double, boolean, etc) and object references pointing to the heap are stored.
Inside any object the same is true. It either contains references to other objects or primitives directly. Objects are always references in any context and those references are passed by value.
So we may have:
[ STACK ] [ HEAP ]
int a: 10; -> MyWrapperObject@21f03b70====||
double b: 10.4; | || int someField: 11 ||
MyWrapperObject@21f03b70 ------| || String@10112222 ----------
...... ||==========================|| |
|
|
String@10112222============||<----
|| ... ||
|| ... ||
}}=========================||
Note that use in some cases(as in via JVM internals) objects may be stored in non-heap memory.
In general local variables are stored on the stack. Instance variables are stored on the heap.
Java works the same way. Be aware that no variable in Java has an object as a value; all object variables (and field) are references to objects. The objects themselves are maintained somewhere (usually on the heap) by the Java virtual machine. Java has automatic garbage collection, so (unlike in C) you don't need to worry about freeing the object. Once all live references to it are out of scope, it will eventually be swept up by the garbage collector.
For instance:
Dog myDog = new Dog();
someMethod(myDog);
passes to someMethod
a reference to the dog object that is referenced by myDog
. Changes to the object that might occur inside someMethod
will be seen in myDog
after the method returns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With