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"
(*Chris*)
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. :)
>
> 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
>
> -----Original Message-----
> From: Thor Heinrichs-Wolpert <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
> Date: Tuesday, August 10, 1999 7:04 PM
> Subject: Re: will this code work?
>
>
> >ummm ... you're wrong here.
> >Yes it will work, as Chris said it would.
> >
> >It follows the basic right hand operation rules, and does indeed printout
> >"my name".
> >
> >I do think it is convoluted and is not clear as to what is being
attempted.
> >The function is returning "my name", it is not over-writing the variable
> >passed in.
> >
> >Passing in "temp" to the function does nothing, but then, it isn't being
> >used to do anything inside of the function.
> >
> >Thor HW
> >----- Original Message -----
> >From: Shiraz Wasim Zaidi <[EMAIL PROTECTED]>
> >To: <[EMAIL PROTECTED]>
> >Sent: Thursday, August 12, 1999 7:06 PM
> >Subject: Re: will this code work?
> >
> >
> >> Hi!,
> >>
> >> No, It wont work....
> >>
> >> The result will be null . Method parameters variables are created
> >> when a method is invoked and its value is initialized with the method
> >> argument.
> >> i.e copy of actual argument is passed not the actual argument.
> >>
> >> Shiraz
> >>
> >> -----Original Message-----
> >> From: Chris Pratt <[EMAIL PROTECTED]>
> >> To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
> >> Date: Thursday, August 12, 1999 7:32 PM
> >> Subject: Re: will this code work?
> >>
> >>
> >> >Yes it will. But that is an awefully convoluted way of doing
> >> >out.println("my name"); At this point I would suggest you read
through
> >the
> >> >Java Tutorial on Sun's site, it's free and it will teach you these
> >> >rudimentary things.
> >> > (*Chris*)
> >> >
> >> >----- Original Message -----
> >> >From: Duke Martin <[EMAIL PROTECTED]>
> >> >To: <[EMAIL PROTECTED]>
> >> >Sent: Thursday, August 12, 1999 4:57 PM
> >> >Subject: will this code work?
> >> >
> >> >
> >> >> String temp = null;
> >> >> temp = process_me(temp);
> >> >>
> >> >> out.println(temp);
> >> >>
> >> >> public String process_me(String temp)
> >> >> {
> >> >> temp = "my name";
> >> >> return temp;
> >> >> }
> >> >>
> >> >> I would like to this the output of this servlet to print "my name"
to
> >the
> >> >> screen. Will this work?
> >> >>
> >> >> thanks
> >> >>
> >> >>
> >>
>
>>__________________________________________________________________________
> _
> >> >> 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
> >>
> >
>
>___________________________________________________________________________
> >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