Monday, July 21, 2008

JavaScript and Java are Pass By Value

Proof that JavaScript and Java are Pass By Value

Pass by value and pass by reference can be daunting at times, and I too have been confused and frustrated at all of the babbling about whether or not Java is one or the other, that is, until I saw an article featuring the Litmus test. I've seen the Litmus test in college, but it didn't make sense completely until reading Scott Stanchfield's "Dammit" article.

Today I was reading Jeff Cogswell's article asserting that JavaScript is pass by value. The blog author is indeed correct in his assertions; however, there were many comments rebutting his arguments. I thought it would be best to support his arguments by applying the Litmus test to JavaScript to prove once and for all whether or not JavaScript is pass by value or pass by reference.

Java is clearly pass by value. Scott Stanchfield's statement says it best: "Objects are not passed by reference. A correct statement would be Object references are passed by value."

The JavaScript Litmus Test

I decided to apply the Litmus test to JavaScript and see what the results are. Here is what I have so far:

function doLitmusTest() {
     var a1 = 5;
     var a2 = 7;
     alert(a1 + ","+a2);

function swap(a,b) {
    var temp = a;
    a = b;
    b = temp;

window.addEventListener("load",function() { doLitmusTest(); },true);

The output is as follows:


The values were not swapped. JavaScript fails the Litmus test, as Java did. This means that JavaScript is also pass by value.

In Java, there is a workaround to being able to implement the swap function, and it also works in JavaScript:

function anotherTest() {
     var obj1 = { value: 5 }; // JSON notation
     var obj2 = { value: 7 }; // JSON notation
     alert(obj1.value + "," + obj2.value);

function swapValue(objA,objB) {
     var temp = objA.value;
     objA.value = objB.value;
     objB.value = temp;

window.addEventListener("load",function() { anotherTest(); },true);

The output is:

In Java, data can be "wrapped" in an object to allow the value to be changed. JavaScript behaves in the same manner. All values, both primitives and object references are passed by value. With primitives, changes we make to one copy of a variable do not affect changes to another copy.

The rule is the same with Object references. Objects are not passed in as parameters in Java or JavaScript, object references are passed as parameters. References point to a specific location in memory. Three references can all point to the same location, so if the data located at that spot in memory is changed, the change will be reflected for all of the references that point to that spot.

However, changes to the reference itself are not reflected. A rule of thumb to remember is that when implementing the swap method, you can only change data by calling setter and getter methods, or by accessing public properties. However, making changes to the object reference instruct the object reference to point to a different location. This is why swapValue worked as expected while swap did not.

Below is a sequence of "visual art" to demonstrate the concepts visually:

new operator uses new memory space

Object A and Object B are instantiated and new memory is allocated on the heap.

using new on object A creates new space in memory

Object A is assigned a reference to new memory space because the "new" keyword was used.

using setter changes value in memory location

The value of the object referenced by B is changed to a new value, but the object reference still points to the same location in memory.

For A, the "new" operator created new space in memory and instantiates a new object. The "12" is located in a different spot in memory.

B, on the other hand, has a value that was set using a setter method. Thus, the value has changed from "7" to "2" without destroying the original object.

multiple objects reference the same memory space
Objects A, C, and D all point to the same place in memory. If a setter method is called on one of the object references, the value is changed in all 3 object references because they all point to the same location in memory.

A good analogy to use to explain this concept is "nicknames". For instance, my name is James, but people also refer to me by Jim. Additionally, I've been called "Mort" before as well. All three names point to the same object, me! In the example I've shown you, A, C, and D are all names of the same object.


Anna said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Course in Chennai

Java Online Training India

mary Brown said...

Angularjs Online Training | Backbone.JS Online Training | Bootstrap Online Training | Node.js Online Training | Typescript Online Training

mary Brown said...

Interesting Article
Javascript Training in Chennai | HTML5 Online Training | JavaScript Online Training

mary Brown said...

Java Training in Chennai | Java Training in Chennai | Dot Net Training in Chennai |
JavaScript Training in CHennai | Java Training Institutes | Single Page Application Development

mary Brown said...

Java Training in Chennai
Online MVC Training India | Angularjs Training | Java Training in Chennai |
Java Training in CHennai | Java Training in CHennai

mary Brown said...

JMS Training Institutes in Chennai | JSP Training Institutes in Chennai | Java Spring Hibernate Training Institutes in Chennai | EJB Training Institutes in Chennai | Hibernate Training Institutes in Chennai

mary Brown said...

JSF Training Institutes in Chennai | Java EE Training Institutes in Chennai | J2EE Training Institutes in Chennai | | | | Core Java Training Institutes in Chennai | Java Training Institutes

mary Brown said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Online Java Training | Java J2EE Online Training | JavaEE Training Institute in Chennai

Devi.Angularjs said...

JavaScript Training in Chennai | Ecmascript 6 Training in Chennai | ES6 Training in Chennai | Angular 2 Training in Chennai | HTML5 Training in Chennai

Yeoman Training | D3 Training | ReactJS Training | Gulp Training | CommonJS Training