Java value transfer and address transfer

Value transfer and address transfer

Ⅰ. Value transfer

  • This method uses variables, constants, and array elements as function parameters. Actually, the value of the actual parameter is copied to the corresponding storage unit of the formal parameter, that is, the formal parameter and the actual parameter occupy different storage units, this transfer method is called "Parameter value transfer" or "function value transfer".
  • The characteristic of value transfer is one-way transfer, that is, when the calling function is called, the storage unit is assigned to the formal parameter, and the value of the actual parameter is transferred to the formal parameter. After the call is completed, the storage unit of the formal parameter is released, and the value of the formal parameter is Any change will not affect the value of the actual parameter, the storage unit of the actual parameter remains and maintains the value unchanged.

Value transfer refers to copying the actual parameters to the function when calling the function, so that if the parameters are modified in the function, the actual parameters will not be affected.

public class Test 
{

	public static void main(String[] args) 
	{		
		String name = "FOREVER_GWC";
		test(name);
		System.out.println(name);	 
	}
	public static void test(String identify) 
	{
		identify = "forever_gwc";
	}
}

Output result:

FOREVER_GWC

Ⅱ. Address delivery

  • In this way, the array name or pointer is used as the function parameter, the first address or pointer value of the array is passed, and the formal parameter receives the address, that is, the storage unit that points to the actual parameter. The formal parameter and the actual parameter occupy the same Storage unit, this transfer method is called "parameter address transfer".
  • The characteristic of address transfer is that there is no storage space for formal parameters, and the compilation system does not allocate memory for the formal parameter groups. The array name or pointer is the first address of a group of contiguous spaces. Therefore, the transfer performed when the array name or pointer is used as the function parameter is only the address transfer. After the formal parameter obtains the first address, it shares a memory space with the actual parameter. The change of the formal parameter is also the change of the actual parameter.

Ⅲ. Reference passing

  • Reference passing refers to directly passing the address of the actual parameter to the function when the function is called, then the modification of the parameter in the function will affect the actual parameter.
public class Test 
{
	public static void main(String[] args) 
	{
		String name[]= {"FOREVER_GWC"};
		test(name);
		System.out.println(name[0]); 
	}
	public static void test(String identify[]) 
	{
		identify[0] = "GWC";
	}
}

Output result:

GWC
Published 19 original articles · praised 0 · visits 1616

Guess you like

Origin blog.csdn.net/FOREVER_GWC/article/details/104854748