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 (28)

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

tydsinbq3, <a href="">gtbmocbvnc</a> , [url=]gwuplthdsh[/url], 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

Posted by anonymous - at Monday, July 8, 2019 8:36 AM airjordanshoser philippemodelsale goldengoosesneakerser goldengoosezapatillas mrgoldengoose nikelunarsale goldengoosesaler nikefreesale saleggdbshoes soldesgoldengoose airmax270site sneakersgoldengoose goldengooseshoesit airvapormaxsale goldengoosesneaker airmax95shoes goldengoosedeluxebransneakers yeezybooststock newgoldengoosesneakers philippemodeloutlet goldengoosesneakersale converse-outlet goldengooser philippemodel ggdbsneakersale

Posted by anonymous - Sneha Basu at Wednesday, July 17, 2019 11:12 AM (mumbai) (ahmedabad) (Vadodara) (Vadodara) (Chennai)


Posted by anonymous - Nagpur escorts service at Friday, November 1, 2019 10:37 PM

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.