Java is always pass by value

Ok, so this is explained ad infinitum elsewhere on the net. It is also, however, a question (is <some language> pass by reference or value?) asked over and over again by newbies.

I’m not going to explain the differences between the two (or 3!) for newbies right now, but what I’m more interested in talking about is whether it is right to refer to Java as being ‘pass by reference’ or ‘pass by value’ – on the assumption that we all know what the concepts involve.

In the Java community it is understood that Java is ‘pass by value’, because Object method arguments (we’ll ignore primitives) take the form of copies of the values of Object references. I.e. another reference to the same object. A method parameter is never the actual Object reference supplied as an argument.

In the Ruby community (I think) the exact same semantics are considered to be ‘pass by reference’, because what you’re actually passing around is always a reference to the Object argument – never the actual Object itself.

So who’s right?

Now, I seem to be constantly telling people that words exist to describe reality – they don’t define reality. And for this reason I’m happy with both the Java & Ruby conventions, as they both make sense given some context. But… there are 2 reasons why I believe that ‘pass by value’ is more precise and useful, and 1 reason why it’s less useful.

In both of these languages, method arguments are eVALUated first, and the result of that evaluation forms the formal method parameter. I believe that the ‘by reference’/’by value’ language is originally compiler terminology and pre-dates both languages, and in such usage ‘by reference’ would refer to the situation where an expression is not evaluated first, and the actual reference is used. For this reason I believe that the Java camp is using the terminology more precisely.

Secondly, differentiating between reference & value allows us to distinguish between standard Java/Ruby semantics, and the ability to actually pass pure references in languages such as C++ (using the & symbol). Clearly this is an important distinction where such references are mutable for the same reasons that it would be important to note whether your local key-cutter was able only to copy house keys – or whether he was also able to alter a key so that it could open someone else’s house! So again the Java communities choice is more meaningful in that sense.

So here’s the downside of describing Java as ‘pass by value’: doing so, obviously, obscures the fact that Java always passes Object arguments as references, and never the actual Object itself. It doesn’t really matter for the most part – but it’s not helpful to new programmers.

So we need a third term! ‘Pass by object sharing’.

Barbara Liskov, who put the L in SOLID, coined the term to avoid these difficulties. ‘Pass by sharing’ describes the semantics without implying anything about the underlying mechanism.

So if someone ever asked be in an interview (I should hope not by now) whether Java was pass by reference or value? I could say ‘yes’. More probably I would say that according to the wider Java community it is ‘pass by value’ – because arguments are evaluated before being used as formal method parameters – but I prefer the term ‘pass by object sharing’.

This entry was posted in coding, java, software and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s