2020-01-02可变参数值传递和参数传递
2020-01-08 本文已影响0人
keeeeeenon
可变参数
package com.hpe.test;
import com.sun.org.apache.regexp.internal.recompile;
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(add(1,2,3));
int []arr={ 1,2,3,4,5};
System.out.println(add(arr));
}
/*可变参数
* 1.可变参数必须是参数列表的最后一个参数
* 2.参数列表中可变参数只能有一个
* 3.可变参数本质上是数组
* 4.在可变参数中传参,既可以传递多个参数,也可以直接传递一个数组
* 5.可变参数在方法内部当成数组使用
*
*/
public static int add(int...args) {
int sum=0;
for (int i : args) {
sum+=i;
}
return sum;
}
}
值传递


引用传递

]





