Re: pass by reference
Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> wrote:
Here, I'm lagging somewhat behind. Where does java "automatic deref"?
Do you mean because method names are appended after a ".", and not after
a "->" as in C with pointers?
It's my point, that java does not auto-deref, but that java's "." is
just C's "->", so the deref is really explicit, but hidden behind a
concise syntax.
Auto-boxing and string concatenation, instanceof, array indexing.
Even in C++, I'm "merely able to modify objects in a subprogram", but in C++
it's still called "pass by reference".
I thought I clearly explained that in C++ you can do modify objects in a
subprogram _both_ with pass by reference and with pass by value?
Back to Java:
...
myMethod(final Object foo) {...}
...
Whatever legal things I do with foo inside myMethod will be reflected
outside. Is *that* "call by ref" then? "foo" inside myMethod is now
just as (un-)retargettable as a reference in C++ is.
No, "pass by reference" doesn't _exist_ in Java. Declaring a method
parameter as final only limits what the subprogram can do with it, it
doesn't change how it's passed into the program.
Consider the following
public class Foo {
public static Widget assignedWidget = new Widget( 1 );
public void doDelayedBar( final Widget w ) {
Thread.sleep( 10000 );
w.bar( );
}
}
Now imagine that we somewhere in our program call doBar(Foo.assignedWidget);
and while that method is waiting in Thread.sleep( ) another thread sets
Foo.assignedWidget = new Widget( 2 );
Which instance of Widget will doDelayedBar( ) call bar( ) on? The first, of
course. But if Java had used call by reference it would have been the
second, because the change to assignedWidget would have been visible inside
of doDelayedBar( ).
--
Leif Roar Moldskred
Got Sfik?