Introduction to Java Call by Value
Calling a function via parameters as a value rather than object or pointers is called “call by value” in any language. In specific to JAVA since there are no pointers used so calling any function by value (not object) can be defined as “call by value”. Here the argument which is passed in the function as input is a variable value stored in memory. There is only “call by value” used in JAVA. Changes done in functions are not impacted by the method of calling the values in the JAVA function.
Syntax
The syntax of “call by value” is used in all the 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. This memory location can be used to store data for it. When the data is stored in the first time value assignment then the manipulation to the same memory location is not supported until the variable is destroyed. For example here:
Int value=5;
The “vale: variable is assigned with number “5” and then in case any change is made on variable “value” then those changes should be stored in a new data variable, not in the same data variable. This functionality can be best understood with the help of the examples provided below.
4.8 (8,467 ratings)
View Course
Examples
Here are the following examples mention 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. Initially, the number “20” was assigned to the input variable. The input variable is then manipulated. But since we are using value here in every function including the printing function so the value of the “input” variable remains unchanged. Here, since no new memory byte allocation is initiated via a new variable declaration and the function is trying to override the same variable named “input”, so the data is not changing at the memory level and still pointing to 20 rather than adding anything to it in output screen.
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. The final product value is stored in a new variable named “product”. The memory byte for a product is again allocated by a system where the multiplication of two values is stored. 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 print function unlike in the previous case. In the previous case since the same variable is being manipulated and no extra memory byte allocated so the value did not change on the output screen.
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 one of the important concepts used irrespective of any language used. Be it JAVA, C, C++, python, or any other, every language uses functions that take one or more parameters to provide a result. There is one more term called “call by reference” which is commonly used when we talk about “call by value”.
“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. This object is an instance of a variable and can be pointed to a new memory location. Because of this capability “call by reference” is used where we need dynamic functionality. While “call by value” is used mostly in non-dynamic conditions and is fast and uses less memory.
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 –