References…almost :)
Another update:
After reading some of the comments to this entry, I googled ‘pass-by-reference’ and discovered that the semantic aspect of what the phrase means has been the topic of many topics along time with funny titles such as “More on Java- pass by reference, but not really” or more seriously “Parameter passing in Java – by reference or by value?” or what I consider the clearest, best articulated article on the topic: Java is Pass-By-Value, Dammit!
(end of update)

Next day update:
OK, thanks go to the first 3 commenters: when venturing into the semantic field, this entry is inaccurate. I should have used this title actually: Java Does Pass-A-Reference because it actually passes the reference’s value.
What does this mean? Well, Java passes a reference to the original object; however it passes a copy of its original reference.
After making this clear, you can keep reading because the point I am making – which is that the language’s behaviour, similar to C’s behaviour, should not come as a surprise – still stands and I am glad to be prompted to clarify it since it makes easier for the reader to grasp why modifying the argument does not change the reference :)
(end of update)

It is simple: either a function/method argument is an object, in which case a reference is passed, or it is not and the argument’s value is passed.
I just read this blog entry that somehow made it to DZone’s home page: Java Does Not Pass-By-Reference.
Well, it is unfortunate. The author makes the case that Java does not pass a reference to an object in a way that would allow him to manipulate said reference.
Of course, this is correct but I find the examples provided somewhat misguided since references themselves are not here to be manipulated but to let you manipulate the object (or, since he mentions C, the memory space) they reference.

Of course, to stay in tune with his C analogy, I should mention that you can modify the reference’s value itself if you use a pointer to a pointer in your function/method signature. For instance, in C, to modify a char* reference, this would look like this:

void myfunction(char **my_double_dereference)
{
    (*mydoublereference) = (char*)malloc(sizeof(char*)*my_size);
}

Now, our original memory space was not modified in the process. However, its reference was lost and replaced with a new reference to a new memory allocation.

C was originally written to be a better macro-assembler. With each new language generation, we are getting further away from the original intent by abstracting the architecture of the machine your program is compiled for. Java is one of the “recent” (Ahem! It’s all relative) languages that tried to free developers from having to worry about pointers. Of course, it quickly became obvious that if, on the one hand, it was easy to write a new program in Java, memory management was not going away and it is very difficult to write clean code if you do not understand its principles. (Note to some commenters: I never wrote that Java does away with pointers)

Note that if you wish to modify references in Java, you can still do that, but you have to be a bit more creative and wrap your reference in another object: this is, for example, how weak references work as you are actually passing a reference to the weak reference’s constructor. Not the object referenced.

Using the same model, let’s rewrite Adam’s Figure 4:

public static void main(String[] args) {
    StringBuffer sb1 = new StringBuffer("Hello");
    StringBufferWrapper wrapper = new StringBufferWrapper(sb1);
    doSomething(wrapper);
    sb1 = wrapper.reference;   

    // what gets printed?
    if (sb1 == null) {
        System.out.println("sb1 is null");
    } else {
        System.out.println("sb1 is not null");
    }
}   

public void doSomething(StringBufferWrapper sb2) {
    sb2.reference = null;
}

I hope I didn’t introduce any mistake in my code, it’s a tad late but you get the idea.

As usual, let me know if this elicits any question on this topic or weak references or what not…
Cheers!

If you enjoyed this post, make sure you subscribe to my RSS feed!