File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Pass By Value (again) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Pass By Value (again)" Watch "Pass By Value (again)" New topic

Pass By Value (again)

Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

Hi everyone.

We have been in the process of deploying some newly minted software. I spent a lot of time tracking down a particular bug and it has confused me regarding what I thought I knew about Java pass by value vs pass by reference. I'll show some of the code, describe the problem, show how I solved it. This is really just more for my edification to make sure I really understand how Java manages objects now...

When I first ran this code, everything looked great. Then I started getting the web developer asking me how come the array kept growing. After staring at the code for some number of hours I did:

Holy Cow!!! It worked. This is a pretty crappy way to get around my problem so once my test discovered it, I fixed it by doing the following:

Given that at no point in the code do I put the temp ArrayList back in my Map as the value associated with the key and I was declaring a new ArrayList, I was caught off guard on this. I thought that Java does not use pointers... What I found in this is that Java is passing me a reference to the object (a pointer), not the value (a copy) of the object. So, when I change the object, despite having it declared in a new reference, the object I do not want to be updated is being updated. Perhaps I'm wrong headed on this but it sure seems to me like Java is using a pointer to my object in memory instead of giving me a new object that I can manipulate. This was easy to solve once I discovered it and it's a lesson learned. I have re-read the article here on this and believe I understand it. But, despite the fact that I can't directly point to some location in memory where an object is stored, isn't Java really just doing that for me when I declare a new reference and therefore is using pointers just like C?

Rob Spoor

Joined: Oct 27, 2005
Posts: 20274

The Map does not contain ArrayList objects, it contains references to ArrayList objects. When you retrieve such a reference and add an element to it, you are adding an element to the ArrayList object the reference is referring to. You are not creating a new ArrayList. As such you do not need to put back the ArrayList into the Map.

Even though it appears that Java has pointers, references are quite different. Pointers refer to actual memory locations whereas references refer to objects. You don't know nor need to know where the object is actually located. Pointer arithmetic is also not possible in Java.

As for the pass by value vs pass by reference - the reference is copied, not the object it is referring to.

Read this and this for more info.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Pass By Value (again)
jQuery in Action, 3rd edition