Who modified my local variable?

dave at thesmithfam.org dave at thesmithfam.org
Tue Jun 13 10:36:58 MDT 2006

On Tue, Jun 13, 2006 at 10:27:50AM -0600, Bryan Sant wrote:
> On 6/13/06, Shane Hathaway <shane at hathawaymix.org> wrote:
> >While I think your intended meaning is correct, I don't believe you're
> >using the term "pass by value" correctly here.  I think the more correct
> >thing to say is that Java passes primitives by value and objects by
> >reference.
> Sure.
> >You're saying Java passes references by value, but that's an unclear
> >term that's hard to distinguish from the idea of passing pointers by
> >value.  C passes pointers by value, which is equivalent to saying C
> >passes pointers.  Unlike passing a reference, passing a simple pointer
> >does not cause a new reference to appear in the object graph, and the
> >unwary C/C++ programmer mixing references and pointers may get an object
> >freed from memory prematurely.
> >
> >So again, I think a better way to express your point is to say that Java
> >passes primitives by value and objects by reference.  Higher level
> >languages like Python do away with primitives and pass everything by
> >reference.
> It's just a matter of terminology.  In Java, when I say:  Object o =
> new Object();
> What is "o"?  An object?  Nope.  It is an "object reference".  That
> reference is always passed by value.

It's this kind of confusion that has prompted me to start using the term
"Pass by Java." The fact is, though, that *everything* in Java is passed
by value. You just have to know that all java.lang.Object derived
objects are created with "new" and thus are pointers. So, when you pass
an Object, Java is passing its pointer by value. This is absolutely NOT
pass by reference, since you cannot reassign the original object's
pointer by modifying it inside the method. This means that Java actually
does not support pass by reference at all, not with primitives and not
with java.lang.Object derived objects either. It only supports pass by
value. It just happens that some values are pointers. Fun, eh? This is
why I prefer C++ where I have total freedom to do "Pass by Java", pass
by reference, or even Objects passed by value (Java can't do either of
the latter two).


More information about the PLUG mailing list