This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Passing Obj Ref vs value and primitives Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Obj Ref vs value and primitives" Watch "Passing Obj Ref vs value and primitives" New topic
Author

Passing Obj Ref vs value and primitives

Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
Another Newbie one:
Class Pi
{
double value = 3.14
public string toString()
{
Double d = new Double(value);
return d.toString();
}
}
public class D
{
public static void main(String[] args)
{
Pi pi = new Pi();
System.out.println("before:" + pi);
zero(pi);
System.out.println("after:" + pi);
}
static void zero(Pi arg)
{
System.out.println("top:" + arg)
arg.value = 0.0;
System.out.println("middle:" + arg)
arg = null;
System.out.println("bottom:" + arg)
}
}
Result=
before: 3.14
top: 3.14
middle: 0
bottom: null
after: 0
My question is why is "after" = 0 and not null?
I've also read somewhere on another thread in here, that the ref var pi would loose ref to that object. However if pi were delclared a primitive ie double pi = 3.14 -vs- Pi pi = new Pi() then in the same scenerio the value of pi wouldnt be changed after the method call, since its only a copy. TIA again!
Stefan Zoerner
Author
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
Hello Paul!
I guess you think it must be "null" because of the line
arg = null;
in your method zero(). The line does not change anything on the object passed to the method. "arg" is a local variable, a reference that can point to an object of type "Pi".
At the start of the method, it refernces to to argument created in main and passed to it. After the line showed above it refences to null, but because "arg" is local to zero, that has no consequence to to calling method main().
Note: The line
arg.value = 0.0;
in zero() uses the value of "arg" like a remote control to change to value of the passed object. That has an effect on the calling method, which uses the same object.
Hope that helped,
Greetings from Hamburg,
Stefan
[ January 18, 2002: Message edited by: Stefan Zoerner ]

Stefan Zoerner
Author of German LDAP-Book
Committer at Apache Directory Project
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
Stephan,
Note: The line
arg.value = 0.0;
in zero() uses the value of "arg" like a remote control to change to value of the passed object. That has an effect on the calling method, which uses the same object.
I can agree with you that the value member variable is now changed to "0" however pi refers to the Pi object. Also why isnt it necessary to invoke toString() ??
For example shouldnt it be written: System.out.println("before:" + pi.toString())
Stefan Zoerner
Author
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
Hello Paul!
I'm back again (sleeping for some hours ...)
To your second question first: "Also why isnt it necessary to invoke toString() ??"
This is a special feature of the "+"-operator in conjunction with String-objcets. An expression like
"Pi = "+pi
calls at evaluation time the toString()-method of the class, pi belongs to, and concatenates the result string with the other String.
You can be sure that this class has a toString()-method, because it is declared in java.lang.Object (root of the class hierarchie). Usually, it is overridden in subclasses to return a textual representation of the objects state (e.g. for debugging purpose), like your class "Pi" does.
You can reed about this special support for converting objects to Strings at
String API
Greetings from Hamburg in the morning,
Stefan
[ January 19, 2002: Message edited by: Stefan Zoerner ]
Stefan Zoerner
Author
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
Hello Paul!
To illustrate what happens in your code here is a small illustration (?).
The variables on the left hand side are refernces to Pi-objects declared in main or zero. The box in the right is the only Pi-object created.

In main after pi = new Pi();

In zero (top):

In zero (middle):

In zero (bottom):

In main (after calling zero)

As you cn see, the value of pi (that is, to which object it references to) is not changed by calling zero(). The state of that value is changed.
Perhaps it helps a bit (using a pencil would be easier, I guess ...),
Stefan
[ January 19, 2002: Message edited by: Stefan Zoerner ]
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
Hi Stephan,
Your illustration was very helpful. Now its my understanding that the toString() is part of the java.lang.Object() root class heirarchy returning the text representation of the object state. Therefore there is no need to invoke toString():
class Pi
{
double value = 3.14
public String toString() //dont need to invoke
{
Double d = new Double(value);
return d.toString(); //this toString is abst
//method of Number
}
}
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Obj Ref vs value and primitives