Results 1 to 7 of 7
  1. #1
    tariqm is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default 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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default 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.

  4. #4
    tariqm is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Passing Primitive Data Type Arguments

    Quote Originally Posted by Tolls View Post
    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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default 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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default 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.

  7. #7
    tariqm is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 04-02-2012, 02:26 PM
  2. Replies: 1
    Last Post: 04-19-2011, 04:32 PM
  3. Primitive data type and class
    By Roselicious in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 03:27 PM
  4. JNI accessing non primitive data type
    By H_P in forum Advanced Java
    Replies: 1
    Last Post: 04-14-2010, 05:43 AM
  5. Uisng primitive type values as keys for Hashtable
    By ravian in forum New To Java
    Replies: 3
    Last Post: 11-21-2007, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •