Re: how java variables are stored?




"Thomas G. Marshall" <tgm2tothe10thpower@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
wrote in message news:zk4Me.1157$zb.479@xxxxxxxxxxx
> Roedy Green coughed up:
>> On Mon, 15 Aug 2005 00:34:17 GMT, "Thomas G. Marshall"
>> <tgm2tothe10thpower@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote or quoted
>>>
>>
>>> This does not answer the OP's question. His question was
>>> specifically (para-phr): Why does the variable need space in *both*
>>> the stack and the heap, and what it means.
>>
>> to answer that question he has to understand the difference between a
>> reference and an object. The reference to a local array can go on the
>> stack but the array object itself goes on the heap.
>
> This does *not* describe the difference between a reference and an object.
> The reference can go on the heap as well the minute it is part of an
> object.
>
> What I'm saying is two fold:
>
> 1. You did not discuss /why/ there necessitates two components (reference
> and data), but that's ok, since your heart is clearly in the right place.
> I have no issues *at all* with your intent.
>
> and
>
> 2. It is nightmarishly hard to do, "depending". (as you seem to agree).

When a friend of mine asks me a question about how some
computer-technology works, I can preface the answer with "Do you REALLY want
to know?" and they've learnt that this a warning indicating that the answer
is very complex and long and that they'd probably be more blissful if they
remained ignorant.

In the case of how the Java memory/variable/storage model works, I try
to find out if they've worked with C, assembler or pointers. If so, then
it's usually straightforward to understand what Java is doing in the
background (ignoring optimizations performed by the garbage collector, the
details of which I am not completely knowlegeable myself either!) Otherwise,
I have to give them a basic introduction to how memory works in computers
(usually I restrict myself to the Intel x86 architecture), and then proceed
with the straightforward explanation mentioned earlier (again, omitting
details about optimizations).

With this strategy in mind, I'd like to ask the Original Poster the
following question:

Do you REALLY want to know?

In all seriousness, in theory, you don't need to know about these
details to be a "good" Java programmer, but in practice maybe people who
understand how the JVM works under the covers find it easier to deal with
otherwise difficult Java concepts. If you want to know because you think
it'd make you a better programmer, then I recommend you go all the way and
take a university level course in microprocessor architectures, do some
programming in MIPS assembler, and then ask your professor to conclude the
course with a broad generalization of virtual machines and how it relates to
the traditional writing code in assembler.

- Oliver


.



Relevant Pages

  • Re: How does managed code work?
    ... Does it work the same way as the native stack with a frame pointer that is the head of a linked list of stack frames where each time we enter a function we create a new stack frame in which new variables are pushed and each time we exit a function the entire stack frame is popped? ... Can someone point me to a discussion of the managed heap? ... How does it prevent memory leaks that occur in COM when two objects reference each other and keep the others reference count nonzero? ... Because objects don't go out of scope, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How java passes object references?
    ... uses the same sort of stack convention as many other languages, including C++ and C#. Local variables are allocated on the stack, not the heap. ... But my understanding is that Java byte-code is compiled "just-in-time" into a platform-efficient representation that is actually executed, and that would mean using platform-supported run-time mechanisms like a stack. ... It's theoretically possible to implement Java with individual heap allocations for each function call, but that would be relatively inefficient and I don't believe any mainstream implementation of Java would do it that way. ... At least then you won't risk confusing people who think you're saying, "pass by reference", which Java doesn't have. ...
    (comp.lang.java.programmer)
  • Re: OO compilers and efficiency
    ... > Objects in Java are heap allocated, ... > no stack allocated objects as there are in C++. ...
    (comp.programming)
  • Re: How java passes object references?
    ... stack and the heap. ... So heap is the actually memory. ... Ironically, the Java heap is actually a little like a stack, in that allocations are always made at the end. ...
    (comp.lang.java.programmer)
  • Re: OO compilers and efficiency
    ... > Objects in Java are heap allocated, ... > no stack allocated objects as there are in C++. ... GC is often faster than manual allocation and deallocation so it isn't ...
    (comp.programming)