Results 1 to 7 of 7
- 04-12-2012, 12:15 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
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
- 04-12-2012, 01:35 AM #2
Re: Passing Primitive Data Type Arguments
Do you have any specific questions?
If you don't understand my response, don't ignore it, ask a question.
- 04-12-2012, 10:29 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Passing Primitive Data Type Arguments
And the text you posted is about references, however your thread title is about primitives?
Please do not ask for code as refusal often offends.
- 04-13-2012, 01:34 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
Re: Passing Primitive Data Type Arguments
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
- 04-13-2012, 01:45 AM #5
Re: Passing Primitive Data Type Arguments
Do you have a question?
If you don't understand my response, don't ignore it, ask a question.
- 04-13-2012, 10:08 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 04-15-2012, 12:58 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Cannot invoke toString() on the primitive type int
By ankiit in forum New To JavaReplies: 5Last Post: 04-02-2012, 02:26 PM -
What is the difference between Integer class and int primitive data type?
By rajkobie in forum New To JavaReplies: 1Last Post: 04-19-2011, 04:32 PM -
Primitive data type and class
By Roselicious in forum New To JavaReplies: 3Last Post: 04-19-2010, 03:27 PM -
JNI accessing non primitive data type
By H_P in forum Advanced JavaReplies: 1Last Post: 04-14-2010, 05:43 AM -
Uisng primitive type values as keys for Hashtable
By ravian in forum New To JavaReplies: 3Last Post: 11-21-2007, 10:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks