GeeCON Prague 2014*
The moose likes Java in General and the fly likes question about passing parameters to the method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "question about passing parameters to the method" Watch "question about passing parameters to the method" New topic
Author

question about passing parameters to the method

Mark Moge
Ranch Hand

Joined: Mar 14, 2009
Posts: 96

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:


SCJP6 | http://programmerutilities.blogspot.com/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Mark Moge wrote:even they are passed as references.

No they're not. Read Call By Reference vs Call By Value.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 14, 2009
Posts: 96

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.
Matthew Cory
Greenhorn

Joined: Sep 27, 2012
Posts: 2

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 ;)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Sep 27, 2012
Posts: 2

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

Joined: Mar 14, 2009
Posts: 96

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 :)
 
GeeCON Prague 2014
 
subject: question about passing parameters to the method