Java – Pass by Value or Pass by Reference?

Plain and simple…

… Java is always pass by value.

However, it’s also true to say that a reference is actually a value – a memory address.  So more accurately Java is pass by value for primitives and pass by an object’s reference, which is a value.

It’s quite interesting that even in Oracle’s Java Programmer certification course it’s written that a primitive is passed by value and an object is passed by reference.  In the case of an object that ‘reference’ is actually a primitive that contains the memory address of where the object structure is held.  From a logical point of view the phrase ‘objects are pass by reference’ actually makes sense, but how would you describe what happens to primitives on a logical level?  Oracle’s wording is actually cause of the confusion.  There is an attempt at distinguishing how primitives are handled i.e primitives are passed by value, compared to objects, but it would much clearer to describe it as is.

From another point of view, this is a fantastic interview question – albeit a very simple one. It does prove if a candidate just remembered some text, or actually thought about it and is able to go into a little bit more detail.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.