Re: How java passes object references?
- From: Lew <lew@xxxxxxxxxxxxx>
- Date: Sat, 26 Apr 2008 22:56:40 -0400
Peter Duniho wrote:
Almost. There's no such thing as "method blocks". I believe (*) Java 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.
Or in registers, although that's one of those details you wisely advise ignoring, since conceptually they live on the stack regardless.
Likewise object allocation, which conceptually happens only in the heap, could happen in practice on the stack or in registers, or not at all. Again, usually ignorable.
But those are implementation details that I think you shouldn't concern yourself with. From a practical point of view, you'll have more success if you realize that local variables are allocated in a fundamentally different way from class instances. The former are allocated on the same stack that's used for function calling, while the latter are allocated from the heap.
And that's the logical truth irrespective of the moment-to-moment optimizations.
(*) I wrote "I believe" because I'm actually relatively new to the Java language. 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.
Your assertions agree with what I've learned so far. The thing about the JIT compilers is that they go beyond the simplistic logical view, inlining method calls and object allocations, unrolling loops, avoiding redundant locks, enregistering all kinds of values, and then de-optimizing all that back to the logical, interpreted way whenever it deems a section no longer worthy of optimization. So the logical view of stack and heap is not necessarily the actual implementation at any given moment, although it could be in the next moment.
So, I'm assuming that C#, as opposed to Java, doesn't have pass-by-
object-reference.
Java doesn't have pass by reference, and "pass-by-object-reference" is not a technical term. If you're going to use approximate or fanciful terms, how about, "Java has pass by pointer"? At least then you won't risk confusing people who think you're saying, "pass by reference", which Java doesn't have.
No. C# has the same pass-by-value semantics that Java has. When you pass an object variable, the value of that variable -- which is a reference to an object -- is passed as a copy, just as in Java. C# _also_ has pass-by-reference, using the "ref" or "out" keywords, but by default C# passes things exactly the same as Java does.
That's what's "as opposed to Java", that C# does support pass by reference.
I think I have just learned a fundamental difference about what a
class variable actually is in Java and, probably, values and
variables. So, what I understood here is that "o" in C++ is either:
A) If it's passed by value, it is a variable that its' value is
Oops, typo. "Its'" should be "Its".
allocated into its'
OK, it wasn't a typo, it was just wrong.
So, Java has something like
an additional "layer" where reference are between a variable and it's
"it's" isn't right, either.
value.
I'm not sure I'd call it an additional layer. It's just a different kind of variable. C++ has variables that reference object instances too; it's just that C++ is more explicit in using them (using the "*" syntax).
[...]In fact, given that Java doesn't support passing by reference, I'm a bit
confused as to why the question wound up here. :) [...]
Well, I as I tried to explain, I'm trying to write a presentation
about what happens under the hood about pass-by-value, pass-by-
reference and (as I think of a better way to name Java's own) pass-
reference-by-value.
Here's the better way: Java has pass by value.
As Peter explains:
Well, one of the reasons I pointed you to Jon's article is that you seem to be breaking the scenarios down into the wrong divisions. In particular, Java's "pass-reference-by-value", as you call it, is really just "pass-by-value". It's no different from "pass-by-value" in any other language (especially when you consider that Java has non-reference types, and other languages have reference types, either of which can be passed "by-value").
There is "passing by reference" and there is "passing by value". As far as parameter passing goes, that's really all anyone should need to know. Java only has "passing by value".
On top of that, there are "reference types" and "value types". Java has both reference and value types, but Java always passes both by value. However, a different language like C++ or C# has both passing by reference and passing by value, and can pass both references and values with by-reference or by-value semantics.
I admit, it's a bit confusing because the words "reference" and "value" are used in two different ways. I don't really mind the disparity myself, but if it's difficult to keep track of, you might try substituting "pointer" and "non-pointer" for the type names. Then you have:
-- pass non-pointer by value
-- pass pointer by value
-- pass non-pointer by reference
-- pass pointer by reference
Java has the first two. C++ and C# have all four.
--
Lew
.
- References:
- How java passes object references?
- From: pek
- Re: How java passes object references?
- From: Peter Duniho
- Re: How java passes object references?
- From: pek
- Re: How java passes object references?
- From: Peter Duniho
- How java passes object references?
- Prev by Date: Re: Is System.in an object?
- Next by Date: Re: How to design proper classes when the requirements are quite complex?
- Previous by thread: Re: How java passes object references?
- Next by thread: Re: How java passes object references?
- Index(es):
Relevant Pages
|