aspose file tools*
The moose likes Beginning Java and the fly likes Passing Reference Data Type Arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Reference Data Type Arguments" Watch "Passing Reference Data Type Arguments" New topic
Author

Passing Reference Data Type Arguments

Rasul Patrick
Greenhorn

Joined: Oct 18, 2012
Posts: 20

Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.

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.

I'm little bit confused please illustrate the texts shown in bold.

Source: Java docs

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8250
    
  23

Rasul Patrick wrote:I'm little bit confused...

The fact is that Java always passes by value; the only difference between reference types and primitives is that, for the former, its the reference that is passed by value. Thus you cannot reassign the value of a passed reference variable, but you can most certainly change the object that it points to if it's mutable (and the presence of a 'set...' method is a bit of a giveaway there).

If you want a better explanation, try the "Use Cups" campfire story (NOTE: it has two parts).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
Rasul Patrick wrote:
Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.

Rasul, which of these concepts gives you a problem?

1. Parameters (received by a method)
2. Scope (of a method)
3. Return (of a method)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rasul Patrick wrote:
Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.




Reference data type parameters, such as objects, are also passed into methods by value.


That's poorly worded. The "such as objects" part is wrong. Objects are not reference data types, objects are never passed to methods, and objects are never stored in variables. Only references are passed and stored. Primitives are passed by value, references are passed by value, and objects are not passed at all.

This means that when the method returns, the passed-in reference stil 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.



A reference variable is like a piece of paper with the address of a house written on it. Passing it to a method is like writing the same address on another piece of paper and handing it to somebody else. Doing x = somethingElse on that variable inside the method is like the second person erasing what was on his paper, and writing a new address on it. That doesn't change what was on the original paper, and it doesn't change the contents of the house.

On the other hand, doing x.someField = somethingElse is like painting one of the rooms of the house (the someFielld part) a different color (the somethingElse part). Both papers still have the same address on them, so anybody using those papers to find that house will see the new wall color.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
Rasul Patrick wrote:I'm little bit confused...

The fact is that Java always passes by value; the only difference between reference types and primitives is that, for the former, its the reference that is passed by value.


There's no difference at all there. In both cases, the value of the variable is copied and passed.

Thus you cannot reassign the value of a passed reference variable,


Just like with primitives.

but you can most certainly change the object that it points to if it's mutable (and the presence of a 'set...' method is a bit of a giveaway there).


There's the difference.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
If you haven’t worked it out yet, and if you have a week to spare, read this thread, where somebody else had a similar problem.
Rasul Patrick
Greenhorn

Joined: Oct 18, 2012
Posts: 20
Thanks for the "cup size story" my problem is now resolved. Thanks to all.
Mohammad Mah
Greenhorn

Joined: Jun 04, 2014
Posts: 4
Thanks for this thread and for those who participated. The cup size story helped for sure.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
You're welcome and welcome to the Ranch.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Passing Reference Data Type Arguments