Updated June 16, 2023
Introduction to Java Call by Value
In any programming language, including Java, when we call a function and pass parameters as values instead of objects or pointers, we refer to it as “call by value.” The specific Java implementation, where pointers are not explicitly used, treats this as “call by value.” In this scenario, the function receives a copy of the variable’s value stored in memory as its argument.
Syntax
The syntax of “call by value” is used in all languages and is more or less similar.
Function: Function_name( parameter1, parameter2)
{
Int variable_name1;
Int variable_name2;
}
Here, function parameters are passed as a value rather than objects.
How Call by Value works in Java?
“Call by value” allocates a data variable to the memory location. The data associated with it can be stored in this memory location. However, manipulating the data within the same memory region after the initial value assignment is not supported unless the variable is destroyed. For example, here:
Int value=5;
Examples
Here are the following examples mentioned below.
Example #1
The below example explains how data is passed using value to a function named addition(). The addition() function will take data as a parameter and will give out manipulated data after adding 200 to it, as we can see in the function definition. But since we are using value here in every function, including the printing function, the value of the “input” variable remains unchanged.
Code:
public class Main {
int input=20;
// The below function will manipulate the data passed to it as parameter value.
void addition(int input){
input=input+200;
}
public static void main(String args[])
{
Main t_var=new Main();
System.out.println("before change "+t_var.input);
t_var.addition(1000);
// Here we pass 500 value instead of any reference.
System.out.println("after change "+t_var.input);
}
}
Output:
Example #2
The below example has a function named “multiply.” This function takes two parameter values and then multiplies these parameters in the functions to provide the final output. Here since we have a new memory byte allocated to store an integer so the value will be successfully stored and displayed on the output screen by the print function, unlike in the previous case.
Code:
public class Main
{
public static void main(String[] args)
{
int a = 30;
int b = 45;
System.out.println("The values we have inputted are: a = " + a + " and b = " + b);
System.out.println("");
multiply(a, b);
System.out.println("Here we are checking that if we pass parameters by value then what will be the product of multiplication of two values.");
}
public static void multiply(int a, int b)
{
System.out.println("Before multiplying the parameters, a = " + a + " b = " + b);
int product = a*b;
System.out.println("After multiplying the parameters, product = " + product);
}
}
Output:
Conclusion
“Call by Value” is a significant concept used in programming languages regardless of the specific language being used. Be it JAVA, C, C++, python, or any other, every language uses functions that take one or more parameters to provide a result. “Call by reference” uses an object rather than the value of the variable itself. We use “call by reference” in dynamic programming since it creates an object of the variable.
Recommended Articles
There is a guide to Java Call by Value. Here we discuss how does Call by Value works in Java with programming examples in detail understanding. You may also have a look at the following articles to learn more –