Java 演示按值调用

Java 演示按值调用

在编程中,函数通常需要传递参数以便调用。调用函数有两种方式,第一种是按引用调用,第二种是按值调用。在本文中,我们将演示Java中的按值调用。

按值调用是一种方法,其中参数的值作为副本传递给函数,因此在函数内部对该参数所做的任何更改都不会影响该参数的原始值,超出该函数的范围。

为了更详细地理解这些概念,我们需要了解用于描述函数的参数类型的两个术语,它们是实际参数和形式参数。形式参数是在函数头中定义的,负责接收实际参数。实际参数是在函数调用期间“实际上”传递给函数的值或变量。

示例

让我们看一个示例来理解实际参数和形式参数。

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 = " + sum);
   }
}

输出

上面的程序将产生以下输出 –

Sum = 15

现在牢记住实际参数和形式参数的概念,重要的是要注意,在按值传递中,实际参数和形式参数在不同的内存位置创建,而在按引用传递中,实际参数和形式参数在相同的内存位置创建,因此它们在实际上是“ 可修改的 ”。

示例

这里有一个简单的示例来演示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);
   }
}

输出

上述程序将产生以下输出 –

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

在Java中,原始数据类型如int, float, char等通常以传值方式传递给函数,而对象则以引用的方式传递给方法。现在我们将演示另一个示例来证明这一点。我们将传递一个int(原始数据类型)和一个int数组(对象)。这也将证明传值和传引用可以在一个函数调用中同时进行。

示例

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 = {5};
      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]);
   }
}

输出

上面的程序将会产生以下输出结果 –

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

在这种情况下,您可以看到在函数调用之前和之后,变量 ‘a’ 的值保持不变,因为它是按值传递的。然而,整数数组 b 这个对象的值在函数调用之后改变了,因为它是通过引用传递的。因此,证明了在Java中,对象是通过引用传递的,而基本数据类型是通过值传递的。

结论

因此,我们已经看到了按值调用的演示,并学到了它是一种在调用函数时不改变参数原始值的技术。而按值调用只适用于基本数据类型,而所有的对象(如数组)都是通过按引用调用的。按值调用中,实际参数和形式参数的内存位置是相同的。在按值调用中,在函数内部进行的所有更改仅在函数的作用域内。值得注意的是,在1个函数调用中,可以同时实现按值调用和按引用调用,就像上面展示的,其中作为参数传递了1个整数和1个整数数组(对象)。最后,按值调用只传递了值的副本。

理解按值调用和按引用调用之间的区别对于编写高效可靠的代码至关重要。然而,总体而言,按值调用更简单易懂且更不容易出错,但在处理大型数据结构的情况下可能会有意想不到的后果。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程