Who modified my local variable?

Shane Hathaway shane at hathawaymix.org
Tue Jun 13 10:00:56 MDT 2006


Bryan Sant wrote:
> But just to clear things up.  Java doesn't pass by reference
> implicitly.  It ALWAYS passes by value.

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.

> So when you pass any
> primitive value, it works just like you would expect in C/C++ -- it
> makes a copy of the var passed into a function (be it a byte, char,
> short, int, double, etc.).  However, when you pass an object REFERENCE
> into a function, it happily copies the reference (4-byte pointer on a
> 32-bit arch) and does not make a wasteful memory copy of object data +
> vtable pointers.

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.

Shane




More information about the PLUG mailing list