Chris, I don't get it--what are you proving here? That I can call obj.add()
and get 10 back? That's precisely what I would expect, and doesn't disprove
what I've claimed earlier.
Look, I think we're getting bogged down in semantics here. When I say that
Java uses Pass-By-Value in its passing of object references, what I mean is
that the *pointer* (the Java reference itself, not the object) is copied,
and the object remains "out there somewhere". I'm not, in any way, claiming
that the entire Object gets copied onto the stack, and any method call on
that object results in a modification of state of that temporary Object
only.
In my earlier example:
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";
}
}
Consider this (crude) pictogram of what's happening in memory. Just before
the call to manipParameter(), the references in question look like this:
temp ---> (Nothing: for purposes of argument, assume address 0x00000000)
Now, we make the call to manipParameter; a new "pointer" to a String type is
created, pushed on the stack, and control jumps to the start of the
manipParameter method. This new "pointer" is called param within the body of
manipParameter:
temp ---> (0x00000000)
param ---> (whatever temp was pointing to, in this case 0x00000000)
When we execute param = "This should be modified", it looks like this:
temp ---> (0x00000000)
param ---> String @ 0x12345678 ("This should be modified")
But because we don't modify the actual "temp" reference itself, temp still
retains its reference pointing to null when it returns!
I really think we're getting caught up in a semantic difference here, and
that if we were to set out our own "Glossaries of terms", we'd find we're
saying the same thing using different terms. I'm a C++-head, originally, so
I can see this in terms of passing the pointers-by-value.
What's more, you're sort of proving my point--you say "you're changing the
value of the reference, not the value of the String", and that's precisely
what I'm trying to prove. You can change the value of the reference, which
won't, in turn, affect the value of the reference used to create the
parameter. Otherwise, if pointing "param" to a new String also affected
"temp" (the source of the parameter to the method), then "temp" would have
the reference to modified contents when we returned out, and it obviously
doesn't.
(And just as a source of nit-picking, I think the second line in test.main
should read process_me(obj), correct?)
If you want to get truly technical about it, Java uses Pass-by-Value for ALL
of its types, including Objects; but because Java only knows about
heap-allocated objects, passing an object "by value" involves passing the
pointer to that Object by value. It's a subtle point, one lost on many Java
developers, but once it's understood, it tends to make a LOT of things a lot
clearer.
Is this any clearer?
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: Chris Pratt <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: Tuesday, August 10, 1999 11:14 PM
Subject: Re: will this code work?
>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);
> }
>
>}
>
___________________________________________________________________________
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