Passing Primitive Data Type Arguments
Hi All,
Can someone Please Shed some light on the following topic, which is taken form the Java Tutorials,
i am sure that many of you have already gone through this tutorial and can elaborate this further
to make is more clearer for me.
Here is the link if you want to check it out, this topic is last topic on this page:
Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Passing Reference Data Type Arguments
Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.
For example, consider a method in an arbitrary class that moves Circle objects:
public void moveCircle(Circle circle, int deltaX, int deltaY) {
// code to move origin of
// circle to x+deltaX, y+deltaY
circle.setX(circle.getX() + deltaX);
circle.setY(circle.getY() + deltaY);
// code to assign a new
// reference to circle
circle = new Circle(0, 0);
}
Let the method be invoked with these arguments:
moveCircle(myCircle, 23, 56)
Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.
Thanks in advance for your help
Tariq
Re: Passing Primitive Data Type Arguments
Do you have any specific questions?
Re: Passing Primitive Data Type Arguments
And the text you posted is about references, however your thread title is about primitives?
Re: Passing Primitive Data Type Arguments
Quote:
Originally Posted by
Tolls
And the text you posted is about references, however your thread title is about primitives?
public void moveCircle(Circle circle, int deltaX, int deltaY)
I think in this method there is one reference type and two primitive types.
In this topic its says that method can take Objects as a parameter, not sure what is the point author is trying to make here though?
Tariq
Re: Passing Primitive Data Type Arguments
Re: Passing Primitive Data Type Arguments
The author is trying to show that the Object (circle) is passed by value, not by reference.
The result of this is that the internals of circle can be changed (by calling setX, setY etc) which will be reflected in myCircle, but any change in the value of the reference (ie pointing the circle variable at another Circle object) will have no effect on myCircle at all.
Re: Passing Primitive Data Type Arguments