Site icon TechVidvan

Call by Value and Call by Reference in Java

In Java, you can make calls that are based on value rather than by reference. This is called by a value when we call methods for passing values. No calls will be affected by the modifications to call methods. When a method is called by reference, the parameter is used as the method’s reference. This will send a reference to an argument to the parameter.

Call by value and Call by reference in Java

Call by value means to call a method with a parameter as the value. This allows the value of an argument to be converted into a parameter. Call by reference implies calling method with parameter as a reference. This results in the parameter being passed as a reference to the argument.

When called by value, the modification of the passed parameter is not reflected in the scope of the caller, while when called by reference, modifications of the passed parameter are persistent and the changes are reflected in the scope of the caller.

Call by value in Java:

The original value shall not change if calls are made by value. We’ll take a simple one,

Example:

class TechVidvan{
    public static void increment(int number){
        number = number+1;  
        System.out.println("value in method: "+number);
    }
    public static void main(String[] args){
        int number=10;
        System.out.println("value before method call : "+number);
        increment(number);
        System.out.println("value after method call: "+number);
    }
}

Call By Reference in Java:

Java only uses calls by value when passing reference variables. It provides a copy of that reference, which is an asset for the methods. As this reference references the same object address, it is not harmful to create a duplicate of that reference. If the new object has been placed in a reference, it will not be reflected.

Example:

public class Vidvan{
   public static void main(String[] args) {
       TechVidvan a = new TechVidvan(30);
        TechVidvan b = new TechVidvan(45);
      System.out.println("Before swapping, a = " + a.a + " and b = " + b.a);
      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(TechVidvan a, TechVidvan b) {
      System.out.println("Before swapping(Inside), a = " + a.a + " b = " + b.a);
      // Swap n1 with n2
      TechVidvan c = new TechVidvan(a.a);
      a.a = b.a;
      b.a = c.a;
      System.out.println("After swapping(Inside), a = " + a.a + " b = " + b.a);
   }
}
class TechVidvan {
   public int a;
   public TechVidvan(int a){ this.a = a;}
}

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

What does the Call by Value mean?

The value of an argument is transferred into a function formally parameter using the call by value method. Therefore, the argument does not change as a result of changing the primary function parameter.

In this parameter passing mechanism, values of real parameters are passed to the function’s formal parameters and they are kept in a variety of memory locations. As a result, the caller’s actual argument does not reflect any changes made in the inside functions.

What does the Call by Reference mean?

The call of the reference technique copies an argument address to a formal parameter. To obtain the true argument of a function call, which is used in this method, use an address. It indicates that any changes made to the parameter will affect a passing argument.

The allocation of memory is the same as the arguments in the present case. The function will run all its operations on a value stored in the real parameter’s location, and an updated value is saved to that address.

The difference between a call of value and a call of reference in Java:

The differences in call by value and call by reference methods of parameter passing are set out in the table below.

                Call By Value                 Call By Reference
While calling a function, we pass the values of variables to it. Such functions are known as “Call By Values”. When you call a function, instead of passing the values of variables, you pass the address of the variable’s location to the function known as the Calling By References function. 
The values of every variable in the call function are copied to corresponding dummy variables on the called function using this method.  This method copies to the dummy variables of the called function the address of the actual variables in the calling function.
This method does not affect the values of real variables in calling function’s calls when you modify a dummy variable within called function.  This method allows us to access the real variables using addresses, which means we can manipulate them.
Using function calls in callbyvalues is not possible to change the values of real variables. If we call by reference using function calls, the variables’ values can be changed. 
The variable values are received using an easy method. A pointer variable must be defined to store fixed address values. 
It is preferable to use this method when we are required to pass some minor values that can’t be changed. It is preferable to use this technique if we wish to transfer a large number of data into the function.

Advantages of Using the Call-by-Value Method:

This approach ensures data is preserved by not altering the initial variable.

Advantages of Using the Call-by-Reference Method:

Disadvantages of Adopting the Call by Value Approach:

Disadvantages of Using the Call by Reference Method:

Conclusion

Both C++ and Java are used in this way according to what’s provided. If you want to see how the original value of a variable changes, use the ‘call by value’ technique or ‘call by reference’ method if you only want to send an initial value.

Exit mobile version