Skip to content
Related Articles

Related Articles

Java Program to Demonstrate the Call By Value
  • Last Updated : 13 Jan, 2021

 Functions can be summoned in two ways: Call by Value and Call by Reference. Call by Value method parameters values are copied to another variable and then the copied object is passed, that’s why it’s called pass by value where actual value does not change

User cases: Calling methods in programming where the method needs to be called for using its functionality. There can be three situations when a method is called or the method returns to the code that invoked it in conditions depicted below:

  1. It completes all the statements in the method
  2. It reaches a return statement
  3. Throws an exception

Remember: Java is Call By Value always

 Implementation: Swapping of numbers is called by value is taken as example to illustrate call by value method

  • Example 1: illustrates the swapping of numbers by creating an auxiliary space in memory known as temporary variable
  • Example 2: illustrates the swapping of numbers by sum and deletion mathematics computations without creating any auxiliary space.

Java




// Java Program showcasing uses of call by value in examples
  
// Importing java input output classes
import java.io.*;
  
// Class
public class GFG {
  
    // Method to swap numbers
    static void swap(int a, int b)
    {
  
        // Creating a temporary variable in stack memory
        // and updating values in it.
  
        // Step 1
        int temp = a;
        // Step 2
        a = b;
        // Step 3
        b = temp;
  
        // This variables vanishes as scope is over
    }
  
    // Main driver method
    public static void main(String[] args)
    {
        // Custom inputs/numbers to be swapped
        int x = 5;
        int y = 7;
  
        // Display message before swapping numbers
        System.out.println("before swaping x = " + x
                           + " and y = " + y);
  
        // Using above created method to swap numbers
        swap(x, y);
  
        // Display message after swapping numbers
        System.out.println("after swaping x = " + x
                           + " and y = " + y);
    }
}
Output



before swaping x = 5 and y = 7
after swaping x = 5 and y = 7

Output explanation: After calling method swap(5,7), integer values 5 and 7 are got copied into another variable. That’s why original value does not change.

Example 2

Java




// Java Program showcasing uses of call by value in examples
  
// Importing java input output classes
import java.io.*;
  
// Class
class GFG {
  
    // Method to update value when called in main method
    static void change(int a)
    {
        // Random updation
        a = a + 50;
    }
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Random assisination
        int a = 30;
  
        // Printing value of varaiable
        // before calling  change() method
        System.out.println("before change a = " + a);
  
        // Calling above method in main() method
        change(a);
  
        // Printing value of varaiable
        // after calling  change() method
        System.out.println("after change a = " + a);
    }
}
Output
before change a = 30
after change a = 30

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :