
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Demonstrate Call by Value in Java
In programming, functions often require parameters to be passed to them 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 that are "actually" passed to the function during the function call.
Different approaches
Following are the different approaches to demonstrate the call by value ?
- Understanding actual and formal parameters with call by value
- Demonstrating call by value with primitive data types
- Using call by value and call by reference together
Understanding actual and formal parameters with call by value
Following are the steps to demonstrate the call by value ?
- Define a method compute(int p, int q) which takes two integer parameters, p and q. These are the formal parameters.
- Within the method, return the sum of p and q.
- In the main method, initialize two integers r and s with values 5 and 10, respectively.
- Call the compute method with r and s as arguments, which act as actual parameters, and store the result in a variable sum.
- Print the value of the sum, which will be the result of adding r and s.
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".
Demonstrating call by value with primitive data types
Following are the steps to demonstrate the call by value in Java ?
- Define a method modifyValue(int value) that takes an integer parameter value.
- Inside modifyValue, print the initial value of the parameter.
- Modify the value by adding 10 to it, and print the new value.
- In the main method, initialize an integer a with a value of 5.
- Print the initial value of a.
- Call modifyValue(a), passing as an argument. Note that only a copy of a is passed.
- Print the value of a again after the function call to show that it remains unchanged.
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.
Using call by value and call by reference together
Following are the steps to demonstrate the call by value in Java ?
- Define a method modifyValue(int value1, int[] value2) that takes an integer value1 and an integer array value2 as parameters.
- Inside the method, print the initial values of value1 and value2[0].
- Modify value1 by adding 10 and update value2[0] by adding 10.
- Print the modified values of value1 and value2[0].
- In the main method, initialize an integer a and an integer array b with values 5 and {5}, respectively.
- Print the initial values of a and b[0].
- Call modifyValue(a, b), passing a and b as arguments.
- Print the values of a and b[0] after the function call to show that a remains unchanged (passed by value), while b[0] is modified (passed by reference).
Example
Here is a simple example to demonstrate call by value in Java ?
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 the 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 learned 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 the same in the 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 a call by value and a call by reference implemented at the same time as shown 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 the 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, 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.