Method - parameter passing, overloading, command-line parameters and the variable parameters

A parameter passing mechanism, the method

  Method of parameter passing mechanism: the argument to parameter. (Argument -> parameter)

  1, the basic data type parameter

    Conclusion: The parameter is the basic data type, parameter arguments passed to the "data value", worth parameter changes do not affect the argument. Since the argument is the "copy copy" to data values of a parameter.

    Graphic:

    

     Note : the stack memory area is divided in accordance with the method, when the method is executed, the method will be assigned to a single region. After machining, the garbage will be recycled.

  2, reference data type parameter

    Conclusion : data type parameter is a reference, pass arguments to the parameter is "address value", the value of the attribute parameter of an object property value modification affects argument object.

    Graphic:

               

Second, the method overloading 

  1, method overload (overload)

    Method overloading : refers to the same class, permits more than one method of the same name , as long as their parameter list different to, the  modifier  and  the return type  independent.

    Parameter list : different numbers, different data types, different order;

    Overloaded method call : JVM by the method's parameter list, calling different methods

  2, method overloading associated with the following factors

    •    Number of different parameters;
    •    Different types of parameters;
    •    Different types of multi-order parameter;

  3, method overloading nothing to do with the following factors

    •  Regardless of the name of the parameter;
    •     And the return value independent of the type;

Third, the command-line arguments

  Command-line arguments: the main method of passing arguments, called the command line parameters.

  Because the main method is a method parameter no return value, so if the main method pass parameters to it?

  Syntax:

java  main所在的类名 实参1  实参2 .....

 

四、可变参数(JDK1.5以后引入)

  可变参数:形参列表中出现了“数据类型... 形参名”形式的形参,就是可变参数。

  包含可变参数的方法的调用原则:

   (1)可变参数的形参部分,可以传入0~n个对应类型的实参,也可以传入对应类型的数组对象

   (2)非可变参数的形参部分,原来该怎么传,现在还怎么传

  可变参数的声明的原则:

   (1)一个方法只能包含一个可变参数

   (2)可变参数必须是形参列表的最后一个

  需求一:求0~n个整数的累加和。

  方式一:

1 public static int sum(int[] nums){
2     int sum = 0; 
3     for(int i=0; i<nums.length; i++){
4         sum += nums[i];
5     }
6     return sum;
7 }

   缺点:每次传入参数的时候必须要声明一个数组,不够方便。

 

   方式二:

1 public static int sum(int... nums){
2     //nums当成数组用
3     int sum = 0; 
4     for(int i=0; i<nums.length; i++){
5         sum += nums[i];
6     }
7     return sum;
8 }

  作为可变参数使用,更加的灵活了,其实nums还是相等于一个数组。但是不能与上面的方式一同时声明使用。

 

    需求二:声明一个方法,功能:可以求1~n个整数的最大值

1 public static int max(int num1, int... nums){
2     int max = num1;
3     for(int i=0; i<nums.length; i++){
4         if(nums[i] > max){
5             max = nums[i];
6         }
7     }
8     return max;
9 }

  注意:int num1就是非可变参数部分,必须传入值;int... nums 是可变参数,而且,可变参数必须是形参列表的最后一个。 

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11870712.html