Java Pass-by-value or pass-by-reference ????

This will give you some insights of how Java really works to the point that in your next discussion about Java passing by reference or passing by value you’ll just smile 🙂

Step one please erase from your mind that word that starts with ‘p’ “_ _ _ _ _ _ _”, especially if you come from other programming languages. Java and ‘p’ cannot be written in the same book, forum, or even txt.

Step two remember that when you pass an Object into a method you’re passing the Object reference and not the Object itself.

  • Student: Master, does this mean that Java is pass-by-reference?
  • Master: Grasshopper, No.

Now think of what an Object’s reference/variable does/is:

  1. A variable holds the bits that tell the JVM how to get to the referenced Object in memory (Heap).
  2. When passing arguments to a method you ARE NOT passing the reference variable, but a copy of the bits in the reference variable. Something like this: 3bad086a. 3bad086a represents a way to get to the passed object.
  3. So you’re just passing 3bad086a that it’s the value of the reference.
  4. You’re passing the value of the reference and not the reference itself (and not the object).
  5. This value is actually COPIED and given to the method.

In the following (please don’t try to compile/execute this…):

1. Person person;  
2. person = new Person("Tom");  
3. changeName(person);  
5. //I didn't use Person person below as an argument to be nice  
6. static void changeName(Person anotherReferenceToTheSamePersonObject) {  
7.     anotherReferenceToTheSamePersonObject.setName("Jerry");  
8. }

What happens?

  • The variable person is created in line #1 and it’s null at the beginning.
  • A new Person Object is created in line #2, stored in memory, and the variable person is given the reference to the Person object. That is, its address. Let’s say 3bad086a.
  • The variable person holding the address of the Object is passed to the function in line #3.
  • In line #4 you can listen to the sound of silence
  • Check the comment on line #5
  • A method local variable –anotherReferenceToTheSamePersonObject– is created and then comes the magic in line #6:
    • The variable/reference person is copied bit-by-bit and passed toanotherReferenceToTheSamePersonObject inside the function.
    • No new instances of Person are created.
    • Both “person” and “anotherReferenceToTheSamePersonObject” hold the same value of 3bad086a.
    • Don’t try this but person==anotherReferenceToTheSamePersonObject would be true.
    • Both variables have IDENTICAL COPIES of the reference and they both refer to the same Person Object, the SAME Object on the Heap and NOT A COPY.

A picture is worth a thousand words:

Pass by Value

Note that the anotherReferenceToTheSamePersonObject arrows is directed towards the Object and not towards the variable person!

If you didn’t get it then just trust me and remember that it’s better to say that Java is pass by value. Well, pass by reference value. Oh well, even better is pass-by-copy-of-the-variable-value! 😉

Now feel free to hate me but note that given this there is no difference between passing primitive data types and Objects when talking about method arguments.

You always pass a copy of the bits of the value of the reference!

  • If it’s a primitive data type these bits will contain the value of the primitive data type itself.
  • If it’s an Object the bits will contain the value of the address that tells the JVM how to get to the Object.

Java is pass-by-value because inside a method you can modify the referenced Object as much as you want but no matter how hard you try you’ll never be able to modify the passed variable that will keep referencing (not p _ _ _ _ _ _ _) the same Object no matter what!

The changeName function above will never be able to modify the actual content (the bit values) of the passed reference. In other word changeName cannot make Person person refer to another Object.

Of course you can cut it short and just say that Java is pass-by-value!


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: