Explanation of java variable number of formal parameter codes

The method of variable number of formal parameters:

1. Format, for the parameter of the method: the name of the parameter of the data type.
2. An overload is formed between the method with a variable number of formal parameters and the method with the same name.

As shown in the following two sections of programs: Three methods constitute overloading. Because a variable number of formal parameter methods are defined, the first two methods can be omitted.

public void sayHello(String str1) {
    
    
	System.out.println("Hello"+str1);
}
public void sayHello() {
    
    
	System.out.println("Hello world");
}
	public void sayHello(String ...args) {
    
    
		for(int i=0;i<args.length;i++) {
    
    
			System.out.println(args[i]);
		}
}

3. When the variable number of formal parameters is called, the number can start from zero to infinite.
4. Is the method of using variable multiple formal parameters consistent with the use of arrays? ? ? ? ?

public void sayHello(String ...args) {
    
    
	for(int i=0;i<args.length;i++) {
    
    
		System.out.println(args[i]);
	}
}
public void sayHello(String[] args) {
    
    
	for(int i=0;i<args.length;i++) {
    
    
		System.out.println(args[i]);
	}
}

5. If there are variable parameters in the method, it must be declared at the end.

public void sayHello(int j,String ...args) {
    
    
		for(int i=0;i<args.length;i++) {
    
    
			System.out.println(args[i]);
		}
	}

6. The position of the formal parameter of a method can only declare at most one variable number of formal parameters

Guess you like

Origin blog.csdn.net/Mr_zhang66/article/details/114339600