aspose file tools*
The moose likes Mock Exam Errata and the fly likes JavaRanch Roud-up - #203 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "JavaRanch Roud-up - #203" Watch "JavaRanch Roud-up - #203" New topic
Author

JavaRanch Roud-up - #203

Wagner Danda
Greenhorn

Joined: Jul 10, 2001
Posts: 26
"(#203)When you pass a variable as an argument to a method call, what are you passing?
Answer: a copy of the value
You always get a copy of whatever is in variable -- either a primitive or a reference. So for objects, you get a copy of the reference."
Is this true? See this example:
class Xxx {
public void incY(Yyy obj) {
obj.id++;
}
}
class Yyy {
public int id = 0;
}
public class TestXxx {
public static void main (String[] args) {
Yyy Yobj = new Yyy();
Xxx Xobj = new Xxx();
System.out.println("Before: " + Yobj.id);
Xobj.incY(Yobj);
System.out.println("After: " + Yobj.id);
}
}
This prints:
"Before: 0
After: 1"
So... Did I forget about something?
[]�s
Wagner Danda


Wagner Danda<br />Sun Certified Programmer for Java 2 Platform
leena rane
Ranch Hand

Joined: Aug 13, 2001
Posts: 280


You always get a copy of whatever is in variable -- either a primitive or a reference. So for objects, you get a copy of the reference."
Is this true?

yes this is absolutely true.
When arguments are pased,a copy of value is passed


class Xxx {
public void incY(Yyy obj) {
obj.id++;
}
}
class Yyy {
public int id = 0;
}
public class TestXxx {
public static void main (String[] args) {
Yyy Yobj = new Yyy();
Xxx Xobj = new Xxx();
System.out.println("Before: " + Yobj.id);
Xobj.incY(Yobj);
System.out.println("After: " + Yobj.id);
}
}
This prints:
"Before: 0
After: 1"
So... Did I forget about something?
[]�s

I have not understanood your question,
i suppose this(from explantion) is what should answer your question
"So for objects, you get a copy of the reference."
if u still have doubt,most welcome !!
Asma Zafar
Ranch Hand

Joined: May 11, 2001
Posts: 49
In java the arguments are always passed by value. But always remember that when the reference to an object is passed as an argument, the reference itself never changes but with the help of this refrence the value of instance/static variables can always be changed.
This is why the output represented by your code is 1 after you passed the reference to the object of YYY.
Hope this helps,

------------------
Asma Zafar,
Sun Certified Programmer for Java2 Platform


Asma Zafar,<BR>Sun Certified Programmer for Java2 Platform
Wagner Danda
Greenhorn

Joined: Jul 10, 2001
Posts: 26
Humm... I think I made a mistake. I was thinking about "copy of the value", and no "copy of the reference". Thanks for the help!

[This message has been edited by Wagner Danda (edited September 08, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaRanch Roud-up - #203