I think that coming up with the right language to describe how this works is
a fascinating subject. This discussion has been enlightening. However, it's
time to let this thread die. It's consuming way too much bandwidth. This is
*NOT* the reason I join this list. I suspect many others feel the same. If I
really want to know more, I have dozens of books I can consult, and several
more appropriate groups to discuss it in.
Please let this thread die!
T.
----- Original Message -----
From: Chris Pratt <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, August 13, 1999 1:04 PM
Subject: Re: will this code work?
> I think you need to look back at those old computer texts, Pass by Value
> means that the Value of the data is placed on the stack and directly
> referenced, so that any changes to the data are not reflected in the data
> that it was copied from. Pass by Reference means that a reference (or
> pointer) to the original data is copied to the stack so that the original
> data may be changed by dereferencine the pointer. No where are you
allowed
> to change the copied data (either the pass by value copy of the data or
the
> pass by reference copy of the reference) and have that change propogated
> outside the called procedure.
> (*Chris*)
>
> ----- Original Message -----
> From: Milt Epstein <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Friday, August 13, 1999 7:14 AM
> Subject: Re: will this code work?
>
>
> > On Thu, 12 Aug 1999, Chris Pratt wrote:
> >
> > > Wrong, Wrong, Wrong. Your example is correct, but that's because
> > > you're changing the value of the reference, not the value of the
> > > String (remember String's are immutable). Java uses Pass by
> > > Reference for Objects and Pass by Value for intrinsic types (int,
> > > float, char, etc). This has been a major source of confusion with
> > > the Java detractors saying that the entire contents of each Object
> > > must be copied to the stack since Java is "pointerless". If you
> > > don't believe me, try this program. I bet you get "i = 10"
> >
> > First: I think you have a mistake in your code, at least in terms of
> > what you are trying to illustrate. The line:
> >
> > obj.add(5);
> >
> > in your main was probably meant to be:
> >
> > process_me(obj);
> >
> > Second: Actually, *you* are wrong. Java does *not* use
> > pass-by-reference, at least not what is traditionally known as
> > pass-by-reference. If it did, and using your example, I could do the
> > following:
> >
> > public class test {
> >
> > public static void process_me(Obj o) {
> > o = new Obj(10);
> > }
> >
> > public static void main(String[] args) {
> > Obj obj = new Obj(5);
> > System.out.println("obj = " + obj);
> > process_me(obj);
> > System.out.println("obj = " + obj);
> > }
> >
> > }
> >
> > and the two println's would show something different. But they won't.
> >
> > It's true that Java uses pass-by-value for the primitive types, but
> > what it uses for Objects is not quite pass-by-reference. It does pass
> > a reference (i.e. pointer) to the object, but you can only use that to
> > manipulate the object indirectly (i.e. through what methods the object
> > allows), not directly (i.e. via assignment). Pass-by-reference allows
> > the latter.
> >
> > You might think that because you can affect the value of an Object
> > parameter, Java is using pass-by-reference, but there are other
> > parameter passing techniques that allow this; pass-by-reference and
> > pass-by-value aren't the only two possibilities.
> >
> > BTW, in an earlier post I said that C's '&' operator allowed
> > pass-by-reference; this isn't technically correct. It's still the
> > value that's being passed, it's just that the value is the address of
> > (i.e. pointer to) a variable, and C's use of pointers allows you to
> > manipulate the contents of a variable through it's pointer (which Java
> > doesn't).
> >
> >
> > > class Obj {
> > > int i;
> > >
> > > public Obj (int i) {
> > > this.i = i;
> > > }
> > >
> > > public int add (int j) {
> > > i += j;
> > > return i;
> > > }
> > >
> > > public String toString () {
> > > return String.valueOf(i);
> > > }
> > >
> > > }
> > >
> > > public class test {
> > >
> > > public static void process_me(Obj o) {
> > > o.add(5);
> > > }
> > >
> > > public static void main(String[] args) {
> > > Obj obj = new Obj(5);
> > > obj.add(5);
> > > System.out.println("obj = " + obj);
> > > }
> > >
> > > }
> > >
> > > ----- Original Message -----
> > > From: Ted Neward <[EMAIL PROTECTED]>
> > > To: <[EMAIL PROTECTED]>
> > > Sent: Thursday, August 12, 1999 10:57 PM
> > > Subject: Re: will this code work?
> > >
> > >
> > > > Folks, the confusion, IMHO, is pretty simple: the difference between
> > > > returning a parameter and the parameter passed in to the method.
> > > >
> > > > For example, had the code been written this way:
> > > >
> > > > public class Param
> > > > {
> > > > public static void main(String[] args)
> > > > {
> > > > String temp = null;
> > > > manipParameter(temp);
> > > > System.out.println("temp = " + temp);
> > > > }
> > > >
> > > > private static void manipParameter(String param)
> > > > {
> > > > param = "This should be modified";
> > > > }
> > > > }
> > > >
> > > > the resulting output would be:
> > > >
> > > > temp = null
> > > >
> > > > because Java supports pass-by-value semantics, not
pass-by-reference,
> even
> > > > for Object-derived types. (This is where Java's insistence that it
has
> no
> > > > pointers really trips people up.) This means that the POINTER to
> String,
> > > > held by temp, is copied into the String reference named "param", and
> > > > subsequent modification of "param" has no effect on the original
> reference
> > > > "temp".
> > > >
> > > > For all you C++-heads out there, the difference is one of
> > > >
> > > > void manipParameter(String* pString); // THIS is what Java does,
> > > effectively
> > > >
> > > > vs.
> > > >
> > > > void manipParameter(String& pString); // THIS is NOT what Java does
> > > >
> > > > Hence, Shiraz is correct in that the *parameter*, temp, (not the
> lvalue of
> > > > the return value's assignment) will not be modified.
> > > >
> > > > However, because the original code was written to not only use the
> > > (wrongly)
> > > > assumed pass-by-reference semantics, but also to copy the return
value
> > > into
> > > > temp:
> > > >
> > > > >> >> temp = process_me(temp);
> > > >
> > > > the code worked as expected.
> > > >
> > > > In short, everybody's right, bur for different reasons. :)
> > [ ... ]
> >
> > 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
> >
>
>
___________________________________________________________________________
> 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
>
___________________________________________________________________________
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