Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does java reference variable stored?

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.

like image 545
Taewan Avatar asked Oct 27 '13 21:10

Taewan


People also ask

Where is a reference variable stored?

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.

What do reference variables store?

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.

Does reference variable store address?

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.

What is the reference variable in Java?

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.


4 Answers

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).

enter image description here

like image 127
Johnny Avatar answered Oct 19 '22 07:10

Johnny


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.

like image 22
nanofarad Avatar answered Oct 19 '22 07:10

nanofarad


In general local variables are stored on the stack. Instance variables are stored on the heap.

like image 2
joews Avatar answered Oct 19 '22 07:10

joews


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.

like image 1
Ted Hopp Avatar answered Oct 19 '22 07:10

Ted Hopp