Java中的参数传递技术和示例介绍

2021年4月16日19:38:03 发表评论 714 次浏览

参数数据可以通过不同的方式传递到方法和函数中。让我们假设函数B()是从另一个函数A()调用的。在这种情况下,A被称为“调用函数”,而B被称为“被调用函数或被调用函数”。同样,A发送给B的参数被称为实际参数,B的参数被称为形式参数。

参数类型:

形式参数:变量及其在函数或方法原型中出现的类型。

语法如下:

function_name(datatype variable_name)

实际参数:对应于形式参数的变量或表达式, 该形式参数出现在调用环境中的函数或方法调用中。

语法如下:

func_name(variable name(s));

参数传递的重要方法

按值传递:对形式参数所做的更改不会传输回调用者。对被调用函数或方法内部的形式参数变量的任何修改只影响单独的存储位置,不会反映在调用环境中的实际参数中。该方法也称为按值调用。

实际上, Java严格按值调用。

Java中的参数传递技术和示例介绍1

例子:

//Java program to illustrate
//Call by Value
  
//Callee
class CallByValue {
  
     //Function to change the value
     //of the parameters
     public static void Example( int x, int y)
     {
         x++;
         y++;
     }
}
  
//Caller
public class Main {
     public static void main(String[] args)
     {
  
         int a = 10 ;
         int b = 20 ;
  
         //Instance of class is created
         CallByValue object = new CallByValue();
  
         System.out.println( "Value of a: " + a
                            + " & b: " + b);
  
         //Passing variables in the class function
         object.Example(a, b);
  
         //Displaying values after
         //calling the function
         System.out.println( "Value of a: "
                            + a + " & b: " + b);
     }
}

输出如下:

Value of a: 10 & b: 20
Value of a: 10 & b: 20

缺点:

  • 存储分配效率低下
  • 对于对象和数组, 复制语义非常昂贵

通过引用致电(别名):

引用调用(别名):对形式参数所做的更改确实通过参数传递传递回调用者。当形式参数接收到对实际数据的引用(或指针)时,对形式参数的任何更改都反映在调用环境中的实际参数中。这个方法也被称为引用调用。这种方法在时间和空间上都是有效的。

Java中的参数传递技术和示例介绍2
//Java program to illustrate
//Call by Reference
  
//Callee
class CallByReference {
  
     int a, b;
  
     //Function to assign the value
     //to the class variables
     CallByReference( int x, int y)
     {
         a = x;
         b = y;
     }
  
     //Changing the values of class variables
     void ChangeValue(CallByReference obj)
     {
         obj.a += 10 ;
         obj.b += 20 ;
     }
}
  
//Caller
public class Main {
  
     public static void main(String[] args)
     {
  
         //Instance of class is created
         //and value is assigned using constructor
         CallByReference object
             = new CallByReference( 10 , 20 );
  
         System.out.println( "Value of a: "
                            + object.a
                            + " & b: "
                            + object.b);
  
         //Changing values in class function
         object.ChangeValue(object);
  
         //Displaying values
         //after calling the function
         System.out.println( "Value of a: "
                            + object.a
                            + " & b: "
                            + object.b);
     }
}

输出如下:

Value of a: 10 & b: 20
Value of a: 20 & b: 40

请注意, 当我们传递引用时, 将为同一对象创建一个新的引用变量。因此, 我们只能更改传递引用的对象的成员。我们不能将引用更改为引用其他对象, 因为收到的引用是原始引用的副本。请参见示例2Java严格按价值传递!


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: