12

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.

Taewan
  • 1,167
  • 4
  • 15
  • 25
  • 4
    In general: Wherever the hell the JVM feels like storing it ;-) Your wording sounds confused though, be sure to distinguish the reference/pointer and the thing that's pointed at/referred to, both in Java and in C! –  Oct 27 '13 at 21:08
  • 1
    @delnan - _"Wherever the hell the JVM feels like storing it."_ is not correct. The [Java Virtual Machine Specification](http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.new) requires that objects be allocated from the garbage-collected heap. – Ted Hopp Oct 27 '13 at 21:15
  • @TedHopp And Sun nicely violates that by offering `sun.misc.Unsafe` which has the ability to store objects off-heap. – nanofarad Oct 27 '13 at 21:16
  • 1
    @TedHopp it is about the reference, not the object. – Ingo Oct 27 '13 at 21:16
  • @TedHopp The JVM spec, like any language spec, includes the as-if rule. When an object doesn't escape or it can otherwise be proven safe, an optimizing implementation is free to put the object on the stack, put some of its members into registers, or even completely elide the allocation. –  Oct 28 '13 at 12:16
  • @delnan - The JVM spec uses "as-if" language in many specific cases, but it pointedly does not use it when describing the operation of the `new` instruction. I don't believe there is a blanket "as-if" rule that would allow a conforming implementation to operate as you describe. (If I'm wrong, I'd appreciate you pointing out where in the spec such a general "as-if rule" can be found.) – Ted Hopp Oct 28 '13 at 14:19
  • @TedHopp I'm not very well versed in the JVM spec, so I wouldn't know how to find an "as if" statement. But even assuming there is none, the wording in the s pec seems to allow these optimizations (why wouldn't it?) -- `new` is defined as "Memory for a new instance of that class is allocated from the garbage-collected heap" (§6.5) and the "heap" can be pretty much anything: "The Java Virtual Machine assumes no particular type of automatic storage management system [...]" (§2.5.3). Also, "The Java Virtual Machine does not mandate any particular internal structure for objects." (§2.7) –  Oct 28 '13 at 17:44
  • @delnan - I think our back-and-forth is a bit of apples and oranges. I'm talking about how Java language elements behave with respect to the (abstract) JVM as defined in the spec. You seem to be talking about how a particular JVM might be implemented in some (concrete) host system. The spec is very clear about where objects go in terms of the structure of the JVM--they never go on a (JVM) stack area; they always exist in the JVM heap. If the JVM heap happens to be implemented on the stack of some underlying OS, that's outside the scope of the discussion, I think. – Ted Hopp Oct 28 '13 at 18:41
  • @TedHopp Ah, up until now I didn't realize you were talking about "heap" and "stack" in the JVM sense. Most people don't and skip right to implementation details. Including, I assume, OP, because otherwise this question is trivial. Yes, in a JVM objects always go to the place the JVM spec calls "heap". –  Oct 28 '13 at 19:11

6 Answers6

25

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.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
4

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

Johnny
  • 14,397
  • 15
  • 77
  • 118
2

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

joews
  • 29,767
  • 10
  • 79
  • 91
1

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

It does work similarly. What is actually happening is that the object itself is stored in the heap, and a reference to the object, which is just like a C pointer, is stored in a local variable.

tbodt
  • 16,609
  • 6
  • 58
  • 83
0

It is exactly the same as a C pointer, and almost always on the stack. In C the object itself is on the heap when you say new Dog(); but the Dog * myDogP pointer (4 or 8 bytes) need not be on the heap and is usually on the stack.

necromancer
  • 23,916
  • 22
  • 68
  • 115