Actually there is no difference between the two, the "Pass by Reference"
method of parameter passing actually passes the Reference to the Object as
the Value on the Stack. Saying that Java Passes the Reference to an Object
as a Value on the stack is very confusing to newbies, but it is correct.
What I was hoping to get to was the proper "Computer Science" definition of
Java's Parameter Passing Methodology, which is "Pass by Reference" for
anything derived from Object, and "Pass by Value" for anything not derived
from Object (i.e. the intrinsic types, such as byte, char, int, etc.)
(*Chris*)
----- Original Message -----
From: Imran Ali Rashid <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, August 13, 1999 4:24 AM
Subject: Re: will this code work?
> Hey All,
>
> I started to write that Chris was right and Ted wrong, but then reread
> Ted's mail 5 or 6 times I think, If you do too, what you will see is
> that in effect whoever's theory you follow, any example will give a
> result that agrees with both of them. An example will prove this.
>
> In the example below, you will that IF you think chris is correct
> everything is straight forward and easy to understand, ON THE OTHER HAND
> IF you think that Ted is correct, THIS IS STILL CORRECT....
> Reason being, that Chris says, the value is passed by reference in which
> basically, the other method gets the original object and does its work,
> while in Ted's case , the pointer is passed(by value) and therefore the
> new object points to the same memory space, meaning that it is
> effectively the same object.
>
> There is BIG difference between the two which I will come to later.
>
> class test
> {
> public static void main(String args[])
> {
> StringBuffer temp = new StringBuffer("Hello");
> addExtraChar(temp);
> System.out.println(temp);//this will output "Hello You"
> file://without the quotes
ofcourse
> }
> public static void addExtraChar(StringBuffer temp)
> {
> temp.append(" You");
> }
> }
>
>
> Now if anyone want to go into the TECHNICAL explanation of why Ted is
> correct and Chris is wrong and I mean technical:
>
> In C++ this was true, I don't know if you can state that this is a
> standard definition....
>
> When passing the pointer (by value) both objects point to the same
> memory space and hence they are effectivelty the same, BUT WHEN PASSING
> BY REFERENCE, the object is NOT passed, but a copy is passed which then
> overwrites the original object(in memory) in the calling method WHEN the
> called method returns(not related to overwritting it your self with
> temp=processme(temp) ).
> If I am wrong above, then disregard the rest of the mail.
>
> Since SUN says, that java does not have pointers, it would seem Ted is
> right. We could ask them or run the example below, which proves my
> point(and Ted's).
>
> IF you were passing by reference, then in the case below, the YOU will
> not be appended to the output string before the the method returned. But
> running it proves that You was appended before the method returned.
> Hence proving Ted correct.
>
> class test
> {
> public static void main(String[] args)
> {
> StringBuffer testValue;
> testValue = new StringBuffer("Hello");
> MyThread a = new MyThread(testValue);
> a.start();
> change(testValue);
> }
>
> private static void change(StringBuffer val)
> {
> val.append(" You");
> try{
> Thread.sleep(2000);
> }
> catch(Exception e)
> {
> System.out.println("Experiment failure");
> }
> System.out.println("change: I haven't returned as yet");
> }
> }
>
> class MyThread extends Thread
> {
> private StringBuffer testValue;
> public MyThread(StringBuffer testValue)
> {
> super();
> this.testValue = testValue;
> file://for the newer programmers note what I did above.
> }
>
> public void run()
> {
> while(true)
> {
> try{
> Thread.sleep(1000);
> } catch(Exception e) {}
>
> System.out.println("From Thread"+testValue);
> }
> }
> }
>
> Hope to hear from you all soon,
> Imran Ali Rashid.
>
>
> Ted Neward wrote:
> >
> > 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:
> >
> > In short, everybody's right, bur for different reasons. :)
> >
> > Ted Neward
> > Patterns/C++/Java/CORBA/EJB/COM-DCOM spoken here
> > http://www.javageeks.com/~tneward
> > "I don't even speak for myself; my wife won't let me." --Me
>
>
___________________________________________________________________________
> 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