Article 68 - Java Pass By Value Reference Demythed

This article is designed to show you why somes experts believe that Java is by value and others think that Java is pass by reference.

Java is neither of the two. The following articles are good reads prior to understanding this topic.

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

Article 65 - Java Pass By Value or By Reference Revisited

We are going to use the following definition.

C and Java Object

Why Some Believe Java is Pass by Value

Passing by value refers to the act of copying the contents of the memory of the variable being passed into the invoked function.

In Java, when we call a method (function), it copies the variable's content onto the stack. This behaviour is identical to the suggested pass by value. Except in Java, variables are Objects (unless they are primitives), and memory of contents of the variable of the Objects is an address.

Pass by value

Thus, when invoking a method, only the address where the Object is stored is copied to the stack, not the entire Object's content. Think of it like C's pointers.

Pass by value example

Many experts confuse this idea because the end behaviour is identical to C's pass by value. But this is not true, in C, the entire memory content is copied onto the stack. So if you had the struct Object, everything is copied, since it is resides in the memory content of where the variable address is. That is, the variable does not hold a memory address but instead is the address of where the struct is residing.

Pass by value Stack

Why Some Believe Java is Pass by Reference

To pass by reference refers to the act of passing in the memory address that acts as an alias of the original variable. However, by definition of a reference, the variable must be defined when the variable is declared, after all, a reference cannot point to another memory address afterwards.

Pass by reference

Pass by reference

Experts confuse this idea, thinking that in Java we can modify the Object's member attributes within a function and the changes are visible in the calling method, is absolutely incorrect. Since it is possible to reassign a Java Object to another Object.

Java is What?

Believe it or not, Java exhibits the following behaviour, except it does not have the full functionality of a pointer.

Pass by pointer

Pass by pointer example

Java is exhibiting the behaviour of passing by pointer.

Comments (25)

Posted by anonymous - hgh reviews at Sunday, February 17, 2013 12:19 AM

It�s really a nice and helpful piece of info. I�m glad that you shared this helpful information with us. Please keep us up to date like this. Thanks for sharing. <a href="http://www.hghreleaserreview.com" title="hgh reviews">hgh reviews</a>

Posted by anonymous - ralph lauren outlet at Thursday, February 21, 2013 8:49 PM

see ralph lauren,Next a few best and newest visits below, nevertheless, is starting to become more and more stylish. ralph lauren outlet http://www.ralphlaurenshop-de.com/

Posted by anonymous - buy discount viagra online at Sunday, March 3, 2013 5:20 AM

DYEl56 Thanks a lot for the article. Will read on...

Posted by anonymous - mxuncnfnje at Friday, March 8, 2013 4:24 AM

tydsinbq3, <a href="http://www.ungycgmndd.com">gtbmocbvnc</a> , [url=http://www.hdsqcbexum.com]gwuplthdsh[/url], http://www.ypmoqrsvdc.com gtbmocbvnc

Posted by anonymous - Miu Miu Outlet at Saturday, March 30, 2013 8:24 AM

I am not sure where you are getting your information, but great topic. I needs to spend some time learning more or understanding more. Thanks for wonderful information I was looking for this information for my mission. Miu Miu Outlet http://miumiuoutlet.v5s7.com

Post a comment

  • Name:
  • Post:
  • Challenge:

Register or login to post comments easier.


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