修饰符 返回值类型 方法名 (参数类型 参数名) {
...
方法体
...
return 返回值;
}
public static int add(int num1, int num2) {
return num1 + num2;
}
int larger = max(30, 40);
System.out.println("Welcome to Java!");
public class VarargsDemo {
public static void main(String args[]) {
// 调用可变参数的方法
printMax(34, 3, 3, 2, 56.5);
printMax(new double[] { 1, 2, 3 });
}
public static void printMax(double... numbers) {
if (numbers.length == 0) {
System.out.println("No argument passed");
return;
}
double result = numbers[0];
for (int i = 1; i < numbers.length; i++)
if (numbers[i] > result)
result = numbers[i];
System.out.println("The max value is " + result);
}
}
The max value is 56.5
The max value is 3.0