Nathan Tenney wrote:

 >Perhaps I am wrong, but I understood passing a
 >reference to an object to be termed pass by
 >reference...

You are correct.  Tom has a minor point in that the Java spec states
that all arguments are, technically speaking, passed by value.  This
statement can be made because every symbolic reference in Java is a 
pointer, never the object itself.

For example, in C++, we can create an object and/or a pointer to an object:

     // creates an object of SomeClass and the symbol refers directly
     // to the object.
     SomeClass theClass;
     theClass.aMethod();
     otherClass.otherMethod(theClass);  // pass by value
     otherClass.otherMethod(&theClass); // pass by reference

     // creates a pointer to a class.  You must always remember it
     // is only a pointer
     SomeClass* thePtr = &theClass;
     thePtr->aMethod();

In Java, the first method does not exist.  There are only "pointers":

     SomeClass theClass;  // creates a pointer to an object
     theClass = new SomeClass();  // creates an object, address goes
                                  // in the pointer.
     theClass.aMethod();   // use dot reference, no special syntax
                           // because it is a pointer
     otherClass.otherMethod(theClass); // passing the object by
                                       // reference, the pointer by value

So, speaking of theClass strictly as a pointer, passing it to a method
involves passing a copy of the contents of theClass to the method.
  Forgetting for the moment that the contents of theClass is an address
to an object, this can be seen as being passed by value.  The value
being passed just happens to be an reference rather than something like
5 or 3.14.

 >--- Tom  Jordan <[EMAIL PROTECTED]> wrote:
 >
 >>You are description of the "fillMe" argument and the
 >>effect of the "fill()"
 >>method is correct, but your assertion that "Java
 >>does use pass by reference"
 >>is WRONG.
 >>
 >>The following statement is absolutely true: "Java
 >>passes all arguments by
 >>VALUE" (references listed below).

When I took an introductory Java course at the local college, the
instructor spent over an hour explaining this to the class, many of whom
were experience programmers.  I know at least half the class didn't get
it because they asked me afterwards to explain it to them.  I cleared up
the confusion in less than five minutes.

I don't relate this to show what a good instructor I am (although I
am!).  It just shows that there is going to be some confusion when you
try to explain the applicable-to-Java-only definition of pass by value,
especially to people who already know the concepts of pass by reference
and pass by value from other languages.

Java is what?  All together now, an *Object Oriented* language.  So, if
we keep our point of view oriented about the objects, we see that Java
*only* uses pass by reference.  There is no way to pass an object to a
method by value.

Having said that, it is important for new Java programmers to realize
that we work *only* with pointers in Java.  We do not directly
refer to the objects themselves.  Once they have internalized that
concept, then we can speak of "passing references by value" without
confusing the hell out of them.  Until then, though, let's show a little
mercy.

Tomm




To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to