On Fri, 13 Aug 1999, Heiko Grussbach wrote:

> Hi,
>
> as indicated before, the confusion stems from not knowing what Java
> passes by reference, and what it passes by value. For your example,
> a reference to a PrintWriter is passed, which is then used. So your
> code works as expected.  But there seems to be also confusion on
> what happens when you change the reference itself in a method.
>
> Everything that inherits from Object is passed by reference, this
> includes String, PrintWriters, anything else despite the basic
> simple types (all types not starting with a capital letter),
> i.e. int, float, double, byte etc. Note, that Integer, Float, Double
> are objects and are passed by reference.
>
[ ... ]
>
> Note, that if you change the reference itself, the situation is much
> the same as with pass-by value, i.e.
>
> public void myMethod(MyObject myObject) {
>      myObject=new MyObject();
> }
>
> will create the same problem, i.e. the newly created object is not
> returned back to the calling method, and is not available anymore
> once myMethod has exited. Thus, such code does not make any sense.

Most of what you say is true, most notably what you say in this last
section.  But note that this is what defines pass-by-reference -- that
is, if pass-by-reference is used, MyObject will be changed in the
calling method.  What Java uses for Objects is not pass-by-reference.

Don't be confused by the fact that Java is passing a reference,
because that isn't what pass-by-reference strictly means.
Pass-by-reference means you can directly (i.e. via assignment in the
called method) cause a change to the variable in the calling method.

Also don't be confused by the fact that in Java, in called methods,
you can cause changes to Objects in the calling method.  You can only
cause these changes indirectly, i.e. via methods on the Object.

Perhaps a problem here is that a lot of these terms
(e.g. pass-by-reference) were defined long before object-oriented
programming was common.  The idea of a variable having methods that
can change it wasn't common.  The changes in technologoy have made the
terms obsolete, or inadequate.  But unfortunately, we can't really go
around changing the meaning of the terms.

Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to