01 logo

Is Java “pass-by-reference” or “pass-by-value”? — Explained

The most asked question in the interview and confuse everyone all the time.

By Rakshit ShahPublished 3 years ago 4 min read
Like
Photo By Author

Java is always pass-by-value, NOT by reference.

Unfortunately, we never handle an object at all, instead of juggling object handles called references (which are passed by value of course).

The chosen terminology and semantics easily confuse many beginners.

This will give you some insights into 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 text.

Step two remember that when you pass 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:

  • A variable holds the bits that tell the JVM how to get to the referenced Object in memory (Heap).
  • 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: 3bPd086a. — 3bPd086a represents a way to get to the passed object.
  • So you’re just passing 3bPd086a, which is the value of the reference.
  • You’re passing the value of the reference and not the reference itself (and not the object).
  • This value is actually COPIED and given to the method.

Now, Compile/execute the below program in your mind or on your console.

Person class | java | Image by Author

What just happened here?

  • 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 3bPd086a.
  • 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 to anotherReferenceToTheSamePersonObject inside the function.
  • No new instances of Person are created.
  • Both “person” and “anotherReferenceToTheSamePersonObject” hold the same value of 3bPd086a.
  • 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.

One picture is worth a thousand words.

Image by Author

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!

AND

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.

Important Key point to keep in mind:

Sometimes people get confused when passing by reference. It’s possible to change the object that the reference refers to (giving the impression of pass-by-reference), but it is not possible to modify the reference itself. So it still remains pass-by-value.

Hope your doubts are clear regarding pass by value or pass by reference.

Get my stories in your feeds by subscribing to me, or become a vocal+ member to read all stories of thousands of other writers, participate in all challenges and get a payout with low fees and less payout threshold on Vocal Media.

© Originally published on 9Mood, also republished on Medium by Rakshit Shah.

interview
Like

About the Creator

Rakshit Shah

I am Computer Engineer and love to make websites and software. I am really eager to know about anything. I am curious to read and write cool stuff.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.