Home Features Programming Pass-by-value vs Pass-by-reference in Java and C++
Pass-by-value vs Pass-by-reference in Java and C++ PDF Print E-mail
Written by FemiByte   
Tuesday, 29 April 2014 03:36

 

Pass-by-value vs Pass-by-reference in Java and C++

 

In this article I illustrate what it means to pass-by-value as opposed to pass-by-reference with a focus on Java vs C++.

The question often asked is this : Is Java pass-by-reference ?
A common and often erroneous answer is : Java is pass by reference for objects, and pass-by-value for primitives.
This is WRONG. To illustrate why this is so, let me refer you to this quote by the father
of Java himself, James Gosling:

Some people will say incorrectly that objects are passed “by reference.” In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory…. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode — pass by value — and that helps keep things simple.

– James Gosling, et al., The Java Programming Language, 4th Edition

The above clearly states that Java passes object references by value meaning that when the reference is passed, a copy of that reference (which is an address) is passed. Since the copy of the reference and the reference refer to the same object, if a call is made to a method that modifies the object in Java, that object is modified, hence the line “Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other”.

I will now illustrate what pass-by-reference means, via a clear example in C++.

Let us create the following files in an appropriate directory with the following contents:

PassByReference.hpp:
#ifndef PassByReference_hpp
#define PassByReference_hpp
void swapIntByRef(int& iParam, int& jParam);
void swapIntByVal(int iParam, int jParam);
#endif

PassByReference.cpp:
#include <iostream>
#include "PassByReference.hpp"
using namespace std;

int main()
{
int i=1000;
int j=2300;

cout << "Illustration of Pass By Reference:\n";
cout << "Before: i= " << i << " j=" << j;
cout << "\n";
swapIntByRef(i,j);
cout << "After: i= " << i << " j=" << j;
cout << "\n";

cout << "\nIllustration of Pass By Value:\n";

i=1100;
j=2500;

cout << "Before: i= " << i << " j=" << j;
cout << "\n";
swapIntByVal(i,j);
cout << "After: i= " << i << " j=" << j;
cout << "\n";

}

void swapIntByRef(int& iParam, int& jParam)
{
int temp(iParam);
iParam=jParam;
jParam=temp;
}

void swapIntByVal(int iParam, int jParam)
{
int temp(iParam);
iParam=jParam;
jParam=temp;
}

We now compile and run the code (assuming you have the g++ compiler):

g++ -o PassByReference PassByReference.cpp
./PassByReference
Illustration of Pass By Reference:
Before: i=1000 j=2300
After: i=2300 j=1000

Illustration of Pass By Value:
Before: i=1100 j=2500
After: i=1100 j=2500

The results above perfectly illustrate the difference between passing by reference vas pass-by-value, at least from the C++ point of view.
By using the reference operator &, when the value of i is passed to the swapIntByReffunction, the actual parameter value is modified in the function such that when the function returns back to the main() function that calls it and the values of i and j are printed out, the values of i and j have been swapped.

In the latter case of pass-by-value, copies of i and j are passed, not references via the & operator.
The result of this is that even though an attempt is made to swap the values in theswapIntByVal function, the original actual parameter values remain unchanged, and this is what we see in the result.

The latter case is what prevails in Java even for all cases, even in the case of objects.

Here is an illustration in Java for both primitives and object references:

Create the file PassByValueDemo.java:

public class PassByValueDemo {

public static void main(String[] args) {
int i=1000;
int j=2300;
System.out.println("Primitives Case");
System.out.println("----------------");
System.out.println(" Before: i=" + i + " j=" + j);
swapInt(i,j);

System.out.println(" After: i=" + i + " j=" + j + "\n");

System.out.println("Wrapper Case");
System.out.println("--------------");
Integer iw=1000;
Integer jw=2300;
System.out.println(" Before: iw=" + iw + " jw=" + jw);
swapInteger(iw,jw);

System.out.println(" After: iw=" + iw + " jw=" + jw);

}

static void swapInt(int iParam, int jParam)
{
int temp=jParam;
jParam=iParam;
iParam=temp;
System.out.println(" iParam=" + iParam + " jParam=" + jParam);

}

static void swapInteger(Integer iParam, Integer jParam)
{
Integer temp=jParam;
jParam=iParam;
iParam=temp;
System.out.println(" iParam=" + iParam + " jParam=" + jParam);
}

}

We now compile and run the code:

javac PassByValueDemo.java

java PassByValueDemo

which produces:

Primitives Case
----------------
Before: i=1000 j=2300
iParam=2300 jParam=1000
After: i=1000 j=2300

Wrapper Case
--------------
Before: iw=1000 jw=2300
iParam=2300 jParam=1000
After: iw=1000 jw=2300

Thus we can see that in both cases of primitive and wrapper classes the values of the actual parameters i and j remain unchanged in the calling routine main. There is no way to achieve the effect we observed in the C++ method PassByRef in Java where the original actual parameters are changed. The underlying object that the reference refers to can be changed via a call to a modifying method on the referenced object, but the reference parameter is always a copy of the original actual parameter.

Summary

  • C++ supports pass-by-value and pass by reference via its & operator.
  • Java supports pass-by-value ONLY. What is erroneously thought of as pass-by-reference is really pass-by-value of an object reference.
Last Updated on Tuesday, 29 April 2014 06:39
 

joomla 1.5 stats