• 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

question about passing parameters to the method

 
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a question regarding passing parameters to a method. Can someone explain me why modifyParameters(Integer v1, Integer v2) doesn't change variables even they are passed as references are references to the objects.

Java code:
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Moge wrote:even they are passed as references.


No they're not. Read Call By Reference vs Call By Value.
 
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
Nothing is passed by reference in Java.

Primitives are passed by value.
References are passed by value. (This is not the same as passing objects by reference, though it may superficially appear so in some ways.)
 
Mark Moge
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, for quick and very useful answer. In fact I had known that parameters in java aren't passed as reference and I've used mistaken words to describe passing a reference (not as reference) to an object. Anyway this two sentences are what I was looking for

If the method changes its copy of the pointer to point to a different object the original pointer is not affected. If the method changes some of the attributes of the object, it changes the original object.

 
Greenhorn
Posts: 2
Eclipse IDE Spring Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Moge wrote:
Can someone explain me why modifyParameters(Integer v1, Integer v2) doesn't change variables even they are passed as references.



Hi Mark,

Actually, what you're doing in that method isn't quite what you're expecting, and it actually doesn't have anything to do with whether or not the objects are passed by reference. What you're actually doing is assigning a new object to the v1 and v2 parameters, so that they're no longer referring to the original objects that were passed in. If you did something like this instead:



That would be closer to what you're expecting, and it would also help explain the difference between pass-by-value and pass-by-reference (at least as I understand it). If Java passed parameters by values, it would create a copy of the object to pass into the method, and you could do whatever you wish to it without having any effect on anything outside of your method:



With passing an object reference to a method, the method has access to the original object itself, and not just a copy of it. In your original code, however, you reassigned the parameters/variables v1 and v2 so that they were no longer pointing at their original objects, but were pointing to the new Integer objects you created.

As a side note, you've probably noticed I've used the "final" keyword on my parameter declarations. The purpose of that is to avoid the exact situation you've come across -- you'll get a compiler error if you try to assign to a parameter that's declared final. That way there's not even a chance that someone comes along and thinks they can modify the object in that way, because 99% of the time it won't be what they're expecting.

I actually had one of my coworkers here -- a really smart guy -- run into this himself the other day and had to explain it there also, so you're not alone ;)
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a good explanation, Matthew (and welcome to the Ranch).

Just one additional comment -- because Mark chose Integer for the type of object being passed, that causes auto-boxing to come into the picture and add to the confusion. It's necessary to observe that



should really be considered as



to remove that confusion.
 
Matthew Cory
Greenhorn
Posts: 2
Eclipse IDE Spring Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Thanks -- and while that was my first post, I've visited the ranch many times over the years; always seems to come up in a google search at least once a day

You're right about autoboxing of course; I honestly hadn't thought to include that in the explanation, but it is definitely something that's always worth considering when working with primitives and wrappers.

Another aspect that I'd meant to mention and forgot about was that the wrapper classes -- such as Integer -- and a few other JDK library classes (String is the main one that comes to mind) are immutable, which is another reason why passing by reference doesn't help matters -- the object's internal data can't be changed anyways. By contrast, the (albeit contrived) class I used is mutable, so its internal data can change. Immutable objects in this case are kinda like passing by value, without the added overhead of creating a copy of the object.

(Getting into all the intricacies of mutability/immutability and passing by reference is probably worthy of a book in it's own, or at least it's beyond the scope of this discussion I imagine, unless someone else has more specific scenarios to work with...)
 
Mark Moge
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Just one additional comment -- because Mark chose Integer for the type of object


I have used Integer instead of int because I thought that reference to it allows to modify the object (and couldn't understand why it didn't work). But it turned out that copy of the reference passed as input parameter was assigned to another object.

Matthew Cory wrote:Another aspect that I'd meant to mention and forgot about was that the wrapper classes -- such as Integer -- and a few other JDK library classes (String is the main one that comes to mind) are immutable


Even the Integer class is immutable, in this case, assigning different Integer object with new value would be ok if you need just a value of it. But as it was explained earlier, method uses copy of the reference which doesn't have any effect on the orginal object, because points to the diffrent one :)

Thanks all of You for replies :)
 
reply
    Bookmark Topic Watch Topic
  • New Topic