Article 5 - Are Java Functions Pass By Value or By Reference

Are Java Functions Pass By Value or By Reference? I'm sure many programmers have asked and researched about that. I have seen answers ranging from pass by value, pass by reference, neither, and both. Out of all the answers I have seen, there was only one article with the correct answer. That author also stated that he came across only one article with the correct answer too. Funny isn't it? What's the correct answer then?

It's neither and it's both. Java's got a hint of each type, but doesn't fulfill the requirements of each type either. What does that mean? Let's find out.

Of the two types, pass by value is simpler to understand. A pass by value function makes a copy of the variable being passed (argument) on the stack. Normally, the stack will contain basic variables, such as return address, arguments, and other language specific variables. In a pass by value fuunction, a copy of the variable is made and changing anything within the copy will not affect the original. In c/c++, the copy constructor is invoked to instantiate this new copy. Hence, inside the called function, if a direct member attribute is modified, it will not affect the original variable. This is true in c/c++, but not in Java. In Java, the memory contents of the variable being copied is duplicated onto the stack. Changing any Object members of the copied object will also affect the original, since only the memory contents have being copied, and thus pointers to deeply nested objects will be retained in Java. Therefore, Java is not purely pass by memory.

Java Pass By Value Example. Source code.

Java Pass By Value Example. Execution result.

Then, let's consider if Java is pass by reference. By definition, passing a variable by reference allows the called function to directly modify the variable. The called function can explicitly reassign this referenced variable to another variable and once the function returns, this pass by reference variable will have been reassigned. This is not the case with Java. In Java, if you reassign an object from the arguments list, you do not reassign the original variable as well. Thus, Java is not pass by reference.

Java Pass By Reference Example. Source code.

Java Pass By Reference Example. Execution result.

We have satisfied the first of the two requirements of the answer, "it's neither" but how is "it's both"?

When we pass a variable to a function in Java, the Java Virtual Machine (JVM) makes a shallow copy of the variable. Like stated, it simply copies the memory contents directly addressed by the object. Hence, it's pass by value, somewhat.

This is confusing! We know, but Java's known for it's beauty of no pointers. Also, this is commonly used for callback functionality, which I will not discuss about.

Next time, you are in an interview, do not get baffled by this question because now you know the correct answer, and how to prove it too!

Comments (20)

Posted by anonymous - hermes bags at Saturday, December 15, 2012 8:21 AM

Hey, you used to write great, but the last few posts have been kinda boring?? I miss your tremendous writings. Past few posts are just a little bit out of track! come on!
[url=http://www.shopauthentichermesbags.com]hermes bags[/url]
<a href="http://www.shopauthentichermesbags.com" title="hermes bags">hermes bags</a>

Posted by anonymous - miumiu at Sunday, February 3, 2013 2:20 AM

find miumiu,chloe,prada is too many type and you may have no idea about such many type. miumiu http://www.miumiusale-jp.com/

Posted by anonymous - ??? at Tuesday, February 5, 2013 1:36 PM

find miumiu,chloe,prada ,Following a few latest visits here, even so, is becoming more and more chic. ??? http://www.chloe-mall.net/

Posted by anonymous - archeage gold at Monday, February 25, 2013 1:17 PM

I am not sure where you're getting your info, but good topic. I needs to spend some time learning much more or understanding more. Thanks for wonderful information I was looking for this information for my mission. archeage gold http://www.cheap-archeage-gold.com/

Posted by anonymous - viagra online at Sunday, March 3, 2013 8:18 AM

9aqXyH A round of applause for your blog.Thanks Again. Much obliged.

Post a comment

  • Name:
  • Post:
  • Challenge:

Register or login to post comments easier.


Vantasy World Copyright 2011 - 2023. Vantasy World is a project developed by Vantasy Online. Privacy Policy.