Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: d207aebaa3
Fetching contributors…

Cannot retrieve contributors at this time

95 lines (59 sloc) 6.161 kB

˝Pass by Reference isn't one of Java's Values

On the interwebs, I noticed some discussions (e.g. proggit and hacker news) about another discussion (serverside) about whether Java the language uses Pass by Reference or Pass by Value. It's an old, old argument, and the tl;dr is that Java and most other modern languages pass references to objects by value, which means it uses Pass by Value. It is absolutely wrong to assert that Java uses Pass by Reference.

While it's tempting to bash Java programmers for the misconceptions, I think that's just confirmation bias. I have absolutely no faith that a statistically significant sample of Ruby or JavaScript programmers would contain a larger proportion of people who get the terms right.

Which, for the most part, probably demonstrates that the issue just isn't that important. If it were, nobody would be able to write code without figuring the names out. The fact is, if you understand what your language does, knowing the correct names is only important when talking to another programmer.

I doubt this comes up much, since two programmers talking about Ruby or Java or javascript would both have the same understanding of what happens with parameters passed to functions/methods.

In my opinion, trouble over terms is more important when moving from a language with one set of semantics to another. For example, when moving from Java to JavaScript, not understanding closures would make for some error-laden conversations with experienced JavaScript programmers.

Duke Mom

But for those of you who haven't seen any of the Aleph-0 explanations, here is the Aleph-One-th explanation of why Java (and Ruby and JavaScript and most other modern programming languages) is Pass by Value.

explanation

Java passes a reference to a mutable object by value. This means:

  1. There is only one instance of the original object, it is not copied, and;
  2. There are two reference to it. One in the original method's environment and one in the called method's environment. Since Java passed the reference by value, there is a copy of the reference in the called method.

Illustration:

public void some_method () {
  String first_ref = "original string";
  // ...
}

So first_ref is a reference to the string "original string" in some_method.

Somewhere else we write:

public void called_method (String second_ref) {
  second_ref = "brand new string";
}

In called_method, second_ref will be a copy of a reference to a String. When called_method is called, second_ref will point to some original String (which is an object, obviously). But it's a brand new reference.

What happens when second_ref = "brand new string"; is executed? Well, first we evaluate the RHS of the assignment. This creates a brand new string with the value "brand new string". Then we change the reference second_ref to point to the brand new string and throw away its original value, which was a reference to the original string.

Let's flesh out some_method:

public void some_method () {
  String first_ref = "original string";
  called_method(first_ref);
  System.out.println(first_ref);
}

What is output?

As above, second_ref inside of called_method now contains a reference to "brand new string". But first_ref hasn't changed, it still points to "original string," and that's what gets printed.

Thus, Java passes references to objects, but it does so by making a brand new reference to the original object. If Java used pass by reference, this program would output "brand new string."

post scriptum

Pass by Reference is rare in modern languages, but consider this line of ancient FORTRAN:

2 = 3

FORTRAN was originally Pass by Reference for everything. To make matters more interesting, integer constants in a program were references to the integers, not some kind of special primitive. Therefore, the above line of code changes the reference 2 to become the reference 3, and everywhere in the program that uses 2, you actually get 3.

Legend has it that this code was used to fix a bug without search and replace, a very expensive operation in the days of paper tape and/or punch cards.

And this just in:

pass by value (of refs) is just so plain better that "by name" and "by reference" are mostly of historic/scientific interest—@axeolotl

True, but every now and then some idiot ventures into the tomb and awakens a long dead monster.


Recent work:


Reg Braithwaite | @raganwald

Jump to Line
Something went wrong with that request. Please try again.