📜  演示按值调用的Java程序

📅  最后修改于: 2022-05-13 01:55:46.995000             🧑  作者: Mango

演示按值调用的Java程序

可以通过两种方式调用函数:按值调用和按引用调用。按值调用方法参数值被复制到另一个变量,然后复制的对象被传递,这就是为什么它被称为按值传递,其中实际值不会改变

用户案例:在编程中调用方法,需要调用该方法以使用其功能。在以下描述的条件下,当一个方法被调用或该方法返回到调用它的代码时,可能存在三种情况:

  1. 它完成了方法中的所有语句
  2. 它到达一个 return 语句
  3. 抛出异常

实现:以数值调用交换数字为例说明按值调用方法

  • 示例 1:通过在内存中创建一个称为临时变量的辅助空间来说明数字的交换
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);
    }
}


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 variable
        // before calling  change() method
        System.out.println("before change a = " + a);
  
        // Calling above method in main() method
        change(a);
  
        // Printing value of variable
        // after calling  change() method
        System.out.println("after change a = " + a);
    }
}


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

输出说明:调用swap(5,7)方法后,整数值5和7被复制到另一个变量中。这就是为什么原始值不会改变的原因。

  • 示例 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 variable
        // before calling  change() method
        System.out.println("before change a = " + a);
  
        // Calling above method in main() method
        change(a);
  
        // Printing value of variable
        // after calling  change() method
        System.out.println("after change a = " + a);
    }
}
输出
before change a = 30
after change a = 30