Java call by value function

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 1 st being call by Reference and the 2 nd 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 result notranslate">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”.

Читайте также:  Get ajax response in html

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 = ; 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.

Источник

Call by value and Call by reference in Java

Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter.

While Call by Reference means calling a method with a parameter as a reference. Through this, the argument reference is passed to the parameter.

In call by value, the modification done to the parameter passed does not reflect in the caller’s scope while in the call by reference, the modification done to the parameter passed are persistent and changes are reflected in the caller’s scope.

Following is the example of the call by value −

The following program shows an example of passing a parameter by value. The values of the arguments remain the same even after the method invocation.

Example — Call By Value

public class Tester< public static void main(String[] args)< int a = 30; int b = 45; System.out.println("Before swapping, a = " + a + " and b = " + b); // Invoke the swap method swapFunction(a, b); System.out.println("
**Now, Before and After swapping values will be same here**:"); System.out.println("After swapping, a = " + a + " and b is " + b); > public static void swapFunction(int a, int b) < System.out.println("Before swapping(Inside), a = " + a + " b = " + b); // Swap n1 with n2 int c = a; a = b; b = c; System.out.println("After swapping(Inside), a = " + a + " b = " + b); >>

Output

This will produce the following result −

Before swapping, a = 30 and b = 45 Before swapping(Inside), a = 30 b = 45 After swapping(Inside), a = 45 b = 30 **Now, Before and After swapping values will be same here**: After swapping, a = 30 and b is 45

Example — Call By Reference

Java uses only call by value while passing reference variables as well. It creates a copy of references and passes them as valuable to the methods. As reference points to same address of object, creating a copy of reference is of no harm. But if new object is assigned to reference it will not be reflected.

public class JavaTester < public static void main(String[] args) < IntWrapper a = new IntWrapper(30); IntWrapper b = new IntWrapper(45); System.out.println("Before swapping, a = " + a.a + " and b = " + b.a); // Invoke the swap method swapFunction(a, b); System.out.println("
**Now, Before and After swapping values will be different here**:"); System.out.println("After swapping, a = " + a.a + " and b is " + b.a); > public static void swapFunction(IntWrapper a, IntWrapper b) < System.out.println("Before swapping(Inside), a = " + a.a + " b = " + b.a); // Swap n1 with n2 IntWrapper c = new IntWrapper(a.a); a.a = b.a; b.a = c.a; System.out.println("After swapping(Inside), a = " + a.a + " b = " + b.a); >> class IntWrapper < public int a; public IntWrapper(int a)< this.a = a;>>

This will produce the following result −

Output

Before swapping, a = 30 and b = 45 Before swapping(Inside), a = 30 b = 45 After swapping(Inside), a = 45 b = 30 **Now, Before and After swapping values will be different here**: After swapping, a = 45 and b is 30

Источник

Java Call by Value

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.

Web development, programming languages, Software testing & others

The syntax of “call by value” is used in all languages and is more or less similar.

Function: Function_name( parameter1, parameter2)

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:

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.

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); >>

Java Call by Value output 1

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.

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.

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 –

500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access

1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access

1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access

3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access

All in One Software Development Bundle 3000+ Hours of HD Videos | 149 Learning Paths | 600+ Courses | Verifiable Certificate of Completion | Lifetime Access

Financial Analyst Masters Training Program 1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access

Источник

Оцените статью