JAVA learning 09 / 22 -- variable number of formal parameters, assignment and formal parameter value transfer

Keywords: Java

Variable number of formal parameters

Java se 5.0 provides the Varargs(variable number of arguments) mechanism, which allows you to directly define formal parameters that can match multiple arguments. Thus, you can pass a variable number of arguments in a simpler way.

  • Method of variable number formal parameters
    1. New content in JDK 5.0
    2. Specific use:
    2.1 format of variable number formal parameters: data type... Variable name
    2.2 when calling the method of variable number formal parameters, the number of parameters passed in can be: 0, 1, 2
    2.3 methods with variable number formal parameters have the same name as those in this class, and methods with different formal parameters constitute overloads.
    2.4 arrays with the same method name and parameter type as those in this class do not constitute overloads. That is, the two cannot coexist.
    2.5 variable number formal parameters must be declared at the end of the formal parameters in the method.
    2.6 variable number parameters in the method, only one deformable parameter can be declared at most.
 */
public class MethodArgs {

	public static void main(String[] args) {
		MethodArgs test = new MethodArgs();
		test.show(12);
		// test.show("hell0");
		// test.show("hello","world");
		// test.show();

		test.show(new String[] { "AA", "BB", "CC" });
	}

	public void show(int i) {

	}

	// public void show(String s){
	// System.out.println("show(String)");
	// }
	public void show(String... strs) {
		System.out.println("show(String ...strs)");


		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);
		}
	}

	// This method cannot coexist with the previous method
	// public void show(String[] strs){
	//
	// }

	public void show(int i, String... strs) {

	}

	//The variable argument type String of the method show must be the last parameter
//	public void show(String... strs,int i,) {
//
//	}
}

Value Passing Mechanism of method parameters

  • On the assignment of variables

If the variable is a basic data type, the data value saved by the variable is assigned at this time.
*If the variable is a reference data type, the assigned value is the address value of the data saved by the variable.

public class ValueTransferTest {

	public static void main(String[] args) {
		
		System.out.println("**********Basic data type:***********");
		int m = 10;
		int n = m;
		
		System.out.println("m = " + m + ", n = " + n);//10,10
		
		n = 20;
		
		System.out.println("m = " + m + ", n = " + n);//10,20

		System.out.println("***********Reference data type:********");
		
		Order o1 = new Order();
		o1.orderId = 1001;
		
		Order o2 = o1;	//After assignment, the address values of o1 and o2 are the same, and both point to the same object entity in the heap space
		System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " + o2.orderId);//1001,1001
		
		o2.orderId = 1002;
		System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " + o2.orderId);1002,1002
		
	}
}

class Order{
	int orderId;
}

Method

Formal parameters: parameters in parentheses declared when a method is defined
Argument: the data actually passed to the formal parameter when the method is called

Value Passing Mechanism:
If the parameter is a basic data type, the parameter is assigned to the real stored data value of the parameter.
If the parameter is a reference data type, the address value of the stored data of the parameter is assigned to the formal parameter

Exercise 1
public class TransferTest3{
	public static void main(String args[]){
		TransferTest3 test=new TransferTest3();
		test.first();
	}
	
	public void first(){
		int i=5;
		Value v=new Value();
		v.i=25;
		second(v,i);
		System.out.println(v.i);
	}
	
	public void second(Value v,int i){
		i=0;
		v.i=20;
		Value val=new Value();
		v=val;
		System.out.println(v.i+" "+i);
		
	}
}
class Value {
	int i= 15;
} 

Output 15 0 20

/*
 * int[] arr = new int[10];
 * System.out.println(arr);//Address value?
 * 
 * char[] arr1 = new char[10];
 * System.out.println(arr1);//Address value?
 */
public class ArrayPrint {

	public static void main(String[] args) {
		int[] arr = new int[]{1,2,3};
        //What is passed in is an Object
		System.out.println(arr);//Address value
		
		char[] arr1 = new char[]{'a','b','c'};
        //What is passed in is an array, which traverses the data
		System.out.println(arr1);//abc
	}
}

Posted by dgs on Wed, 22 Sep 2021 23:22:18 -0700