Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Pass-by-value Semantics in Java Applications

By Peter Haggar
2003-11-24


Examples

Using the preceding definitions and discussion, we will analyze some examples. First consider some C++ code. The C++ language uses both pass-by-value and pass-by-reference argument passing mechanisms:Listing 1: C++ example


#include
#include

void modify(int a, int *P, int &r);

int main (int argc, char** argv)
{
int val, ref;
int *pint;

val = 10;
ref = 50;
pint = (int*)malloc(sizeof(int));
*pint = 15;

printf("val is %d\n", val);
printf("pint is %d\n", pint);
printf("*pint is %d\n", *pint);
printf("ref is %d\n\n", ref);

printf("calling modify\n");
//val and pint passed by value, ref is passed by reference.
modify(val, pint, ref);
printf("returned from modify\n\n");

printf("val is %d\n", val);
printf("pint is %d\n", pint);
printf("*pint is %d\n", *pint);
printf("ref is %d\n", ref);

return 0;
}

void modify(int a, int *p, int &r)
{
printf("in modify...\n");
a = 0;
*p = 7;
p = 0;
r = 0;
printf("a is %d\n", a);
printf("p is %d\n", p);
printf("r is %d\n", r);
}

The output of this code is:

Listing 2: Output of C++ code


val is 10
pint is 4262128
*pint is 15
ref is 50

calling modify
in modify...
a is 0
p is 0
r is 0
returned from modify

val is 10
pint is 4262128
*pint is 7
ref is 0

This code declares three variables: two ints and one pointer. Each variable is set to an initial value and then printed. The pointer value is printed along with what it points to. All three variables are then passed as parameters to the modify function. The first two parameters are passed by value and the last parameter is passed by reference. The function prototype for the modify function shows that the last parameter is to be passed as a reference. Recall that C++ passes all parameters by value, except references, which are passed by reference.

The modify function changes the values of all three parameters:

  • The first parameter is set to 0.
  • The value the second parameter points to is set to 7, then the second parameter is set to 0.
  • The third parameter is set to 0.

The new values are printed, then the function returns. When execution returns to main, the values of the three variables, along with the value the pointer points to, are printed again. The variables passed as the first and second parameters are not affected by the modify function because they were passed by value. The value that the pointer points to did change, however. Notice that unlike the first two parameters, the variable passed as the last parameter is changed by the modify function because it is passed by reference.

Now consider similar code written in the Java language:

Listing 3: Java application


class Test
{
public static void main(String args[])
{
int val;
StringBuffer sb1, sb2;

val = 10;
sb1 = new StringBuffer("apples");
sb2 = new StringBuffer("pears");
System.out.println("val is " + val);
System.out.println("sb1 is " + sb1);
System.out.println("sb2 is " + sb2);
System.out.println("");

System.out.println("calling modify");
//All parameters passed by value
modify(val, sb1, sb2);
System.out.println("returned from modify");
System.out.println("");

System.out.println("val is " + val);
System.out.println("sb1 is " + sb1);
System.out.println("sb2 is " + sb2);
}

public static void modify(int a, StringBuffer r1,
StringBuffer r2)
{
System.out.println("in modify...");
a = 0;
r1 = null; //1
r2.append(" taste good");
System.out.println("a is " + a);
System.out.println("r1 is " + r1);
System.out.println("r2 is " + r2);
}
}

The output of this code is:

Listing 4: Output of Java application


val is 10
sb1 is apples
sb2 is pears

calling modify
in modify...
a is 0
r1 is null
r2 is pears taste good
returned from modify

val is 10
sb1 is apples
sb2 is pears taste good

This code declares three variables: one int and two object references. Each variable is set to an initial value and printed. All three variables are then passed as parameters to the modify method.

The modify method changes the values of all three parameters.

  • The first parameter, the int, is set to 0.
  • The first object reference, r1, is set to null.
  • The second object reference, r2, retains its value but changes what it refers to by calling the append method. (This is analogous to what was done with the pointer, p, in the previous C++ example.)

When execution returns to main, the values of the three variables are printed again. The int val is unchanged as expected. The object reference sb1 is also unchanged. If sb1 was passed by reference, as many people claim, it would be null. However, since the Java programming language passes all parameters by value, a copy of the reference for sb1 is passed to the modify method. When the modify method set r1 to null at //1, it was doing so on a copy of the sb1 reference, not the original as was done in C++.

Also, note that the second object reference, sb2, prints out the new string that was set in the modify method. Even though the variable r2 in the modify method is a copy of the reference sb2, they both refer to the same object. Therefore, methods called on the copied reference change the same object.



Tutorial Pages:
» Pass-by-value semantics in Java applications
» The key point
» Parameter passing in C++ and Java applications
» Examples
» Writing a swap method
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT