Java Program to Demonstrate the Call By Value


In programming, functions often require parameters to be passed to them in order to be called or invoked. There are 2 ways to call functions, the 1st being call by Reference and the 2nd being call by value. In this article, we are going to demonstrate call by value in java.

Call by value is a method in which the value of the argument is passed as a copy to the function, hence any change made to this argument inside the function will not affect the original value of the argument beyond the scope of this function.

To help understand these concepts in detail, we need to understand the 2 terms that are used to describe the type of parameters or arguments of the function. They are actual parameters and formal parameters. Formal parameters are those which are defined in the function header and are responsible for receiving the actual parameters. Actual parameters are those values or variables which are “actually” passed to the function during function call.

Example

Let us see an example to understand actual and formal parameters.

public class Main {
   public static int compute(int p, int q) {
      // These 2 variables, p and q are the formal parameters
      return p+q;
   }
   public static void main(String[] args) {
      int r=5, s=10;
      int sum = compute(r, s); //r and s  here are actual parameters
      System.out.println("Sum = " + sum);
   }
}

Output

The above program will produce the following output -

Sum = 15

Now keeping in mind, the concept of actual and formal parameters, it is important to note that in call by value, the actual and formal parameters are created at different memory locations while in call by reference, the actual and formal parameters are created at the same memory locations and hence are in sync or actually “modifiable”.

Example

Here is a simple example to demonstrate call by value in java.

public class Main {
   static void modifyValue(int value) {
      System.out.println("The value of the variable inside of the function before any modifications is " + value);
   
      value = value + 10;
      System.out.println("The value of the variable inside of the function after adding 10 to it is " + value);
   }
   public static void main(String[] args) {
      int a = 5;
      System.out.println("The value of variable a, before calling the function is " + a);
   
      //invoke the function and pass the argument 
      modifyValue(a);
      System.out.println("The value of variable a, after calling the function is " + a);
   }
}

Output

The above program will produce the following output -

The value of variable a, before calling the function is 5
The value of the variable inside of the function before any modifications is 5
The value of the variable inside of the function after adding 10 to it is 15
The value of variable a, after calling the function is 5

In java, primitive data types like int, float, char etc are normally passed to functions using call by values whereas objects are passed to methods as references. Now to prove this point we are going to see another example below. Here we will be passing 1 int (primitive data type) and 1 int array (an object). This will also prove that call by value and call by reference can go hand in hand in 1 single function call.

Example

public class Main {
   static void modifyValue(int value1, int[] value2) {
      System.out.println("The values of value1 and value2[0] are "+value1+" and "+value2[0]+" inside the function before any modifications");
      value1 = value1 + 10;
      value2[0] = value2[0] + 10;
      System.out.println("The values of value1 and value2[0] are "+value1+" and "+value2[0]+" inside the function after adding 10 to them");
   }
   public static void main(String[] args) {
      int a = 5;
      int[] b = {5};
      System.out.println("The value of variable a and b[0], before invoking the function is " + a+" and "+b[0]);
   
      // call the function and pass both parameters as a and b respectively
      modifyValue(a, b);
      System.out.println("The value of variable a and b[0], after invoking the function is " + a+" and "+b[0]);
   }
}

Output

The above program will produce the following output -

The value of variable a and b[0], before invoking the function is 5 and 5
The values of value1 and value2[0] are 5 and 5 inside the function before any modifications
The values of value1 and value2[0] are 15 and 15 inside the function after adding 10 to them
The value of variable a and b[0], after invoking the function is 5 and 15

In this case you can see the value of ‘a’ remains same before and after the function call since it is passed by value. However, the value of the integer array b which is an object is changed after the function call since it is passed by reference. Hence it is proved that in java, objects are passed by reference while primitive data types are passed by value.

Conclusion

We have therefore seen the demonstration of call by value and learnt that it is a technique to call functions in which the original value of the parameter is not changed. Further call by value is only limited to primitive data types and all objects like arrays are passed by call by reference. The memory location for the actual and formal parameters is same in call by value. All the changes made inside the function in call by value remain only within the scope of the function. It is worth noting that in 1 function call, there can be call by value and call by reference implemented at the same time as show above where 1 integer and 1 integer array (an object) were passed as parameters to the function. Lastly, only a copy of the value is passed in call by value.

Understanding the difference between the call by value and call by reference is essential in order to write efficient and reliable code. However, on the overall, call by value is simpler to understand and less prone to errors but can have unexpected consequences in scenarios where we are dealing with large data structures.

Updated on: 10-Mar-2023

274 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements