Over a million developers have joined DZone.

Java, C#, and JavaScript Call by Value and Reference

DZone's Guide to

Java, C#, and JavaScript Call by Value and Reference

Need a quick guide for doing so? Check out Mohsen Salehi's article about it.

· Web Dev Zone
Free Resource

Get the senior executive’s handbook of important trends, tips, and strategies to compete and win in the digital economy.

We have two simple methods — one method in Java one method in C#. Both of these two methods get one parameter and return parameter+1 after it. I changed this sample for testing an object input not primitive value.

C# (Part I)    


Java (Part I)

image02JavaScript (Part I)



Also, you can see our parameter does not change after call function sum.

We change param type from integer to a class with a one property as integer.

C# (Part II)


Java (Part II)


JavaScript (Part II)



Now we change the code and create new instance in sum method of input object:

C# (Part III)


Java (Part III)


Now I change call by value to reference:

C# (Part IV)


Java (Part IV)


So we have an error here. You simply can’t do that in Java, since Java doesn’t support pointers …

But in JavaScript:

  1. JavaScript is always pass by value, but when a variable refers to an object (including arrays), the “value” is a reference to the object.
  2. Changing the value of a variable never changes the underlying primitive or object, it just points the variable to a new primitive or object.
  3. However, changing a property of an object referenced by a variable does change the underlying object.

Read this guide to learn everything you need to know about RPA, and how it can help you manage and automate your processes.

java ,c# ,java script

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}