• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing Reference Data Type Arguments

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the "cup size story" my problem is now resolved. Thanks to all.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for this thread and for those who participated. The cup size story helped for sure.

 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome and welcome to the Ranch.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic