DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Java, C#, and JavaScript Call by Value and Reference

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

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

Mosen Salehi user avatar by
Mosen Salehi
·
Oct. 03, 16 · Tutorial
Like (4)
Save
Tweet
Share
3.42K Views

Join the DZone community and get the full member experience.

Join For Free

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)    

image00

Java (Part I)

image02JavaScript (Part I)

image10

image03

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)

image08

Java (Part II)

image05

JavaScript (Part II)

image04

image07

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

C# (Part III)

image06

Java (Part III)


image09

Now I change call by value to reference:

C# (Part IV)

image01

Java (Part IV)

image11

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.
csharp Java (programming language) JavaScript Object (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Memory Debugging: A Deep Level of Insight
  • Secrets Management
  • Apache Kafka vs. Memphis.dev
  • Core Machine Learning Metrics

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: