In Java parameter passing in the end it is passed by value or reference semantics, and explored the deep! ! !

In Java parameter passing in the end it is passed by value or reference semantics, and explored the deep! ! !

We need to clear two important points before watching this article!
1. What is the parameter, what is the argument?

Parameter
is defined: full name of "formal parameters", when used to define the parameters of the method used, for the purpose of receiving the parameters passed when calling the method.
Description: The unit will only allocate memory when it is called, in the end of the call, immediately release the allocated memory unit. Therefore, the only valid within the method.

Argument
is defined: full name of "actual parameter", when used to transfer the call to the method parameters, i.e. the value passed to the called method.
Description: pre-created and given to determine the value.

2. What is the stack? What is the heap?

What is the stack memory?
Stack memory is another Java memory is mainly used to perform procedures, such as: basic types of variables and object reference variables.
Stack memory characteristics?
The first point : the stack memory like a bucket attached to the pie, entered into the pie, so to put it in the bottom of the barrel, then when you want to remove the pie, was from remove the top down gradually, so it is characterized by: last out, last in first out.
The second point : access speed faster than the reactor, after register, stack data can be shared, but the disadvantage is that there is data in the stack size and survival must be determined, the lack of flexibility.

What is the heap?
Java heap memory is a memory, its role is to store java objects and arrays, when we create a new array or an object and they will open up some space in the heap memory to it, for storage.
What features are heap memory?
The first point : the stack can in fact be seen as normal to queue up for tickets, you have to line up, you first buy a ticket first row, back row after ticket. So heap memory is characterized by: the FIFO, the backward,
the second point : the size of heap memory can be allocated dynamically, survival do not have to tell the compiler in advance, because it is at run-time dynamic allocation of memory, but the drawback is , due to dynamic allocation of memory access slower at runtime.

Conceptual understanding clearly, directly on the code! ! !
First, the basic types of parameters passed Case! ! !

//基本类型参数传递案例
package exercise;
public class exercise56 {
	public static void swap(int value1, int value2) {
		int temp=value1;
		value1=value2;
		value2=temp;
		System.out.println("在swap方法中进行值交换后的value1:"+value1+" 进行值交换后的value2:"+value2);
	}
	public static void main(String[] args) {
		int value1=10;
		int value2=20;
		System.out.println("进行值交换前的value1:"+value1+" 进行值交换前的value2:"+value2);
		swap(value1,value2);   
		System.out.println("执行swap方法过后的value1:"+value1+" 进行值交换后的value2:"+value2);
	}
}

//运行结果
//进行值交换前的value1:10 进行值交换前的value2:20
//在swap方法中进行值交换后的value1:20 进行值交换后的value2:10
//执行swap方法过后的value1:10 进行值交换后的value2:20

Analysis : We wish to exchange value value1 and value2 after calling the swap () method, but the fact is not, this is how it happened? In value1 swap stack area of the main stack area is non-interfering with value2, value1 with value2 swap main stack area is only a copy of it, which means that there are five variables! Are swap () stack area temp, value1, value2 with the main stack area value1, value2. If you still can not understand the words, it can be understood, we have heard about parallel universes, each of which has a parallel universe two people (value1 with value2), but their fate is different, that is, parallel universe interference between the cosmos! So this is why there is no reason for the success of the exchange value no!

A First Look at the memory map
Here Insert Picture Description
jvm in the end what did it?

The first step : jvm to create a main stack area, stack area to define and initialize value1, value2 re-define and initialize, the two arguments passed to swap () method.
Step Two : JVM stack creating the swap area, the stack area is defined in two parameters, VALUE1 and value2, to receive the incoming two values, the reception success becomes parameter argument. Redefinition temp variable, the value assigned to the temp of value1, value2 value is then assigned to value1, and finally to assign a value of temp value2, the switching operation is completed in the swap stack area. Everything will swap the stack area, from top to bottom are popping operation, swap stack area disappear.
The third step : main method is finished, the main area of the stack everything, from top to bottom are popping operation, main stack area disappear.

Conclusion : The basic parameters are passed in java type is passed by value, the value of the transfer is actually a copy of the actual parameter passed in the method, and the parameter itself will not be affected.

More than words but still hope that we can understand the deep value is passed, then the code! ! !
And then pass on the reference type parameter case! !
!

//引用类型参数传递
package exercise;
public class exercise59 {
	int value1;
	int value2;
}

package exercise;
public class exercis58 {
	public static void  swap(exercise59 e){
		int temp=e.value1;
		e.value1=e.value2;
		e.value2=temp;
		System.out.println("在swap方法中进行值交换后的value1:"+e.value1+" 进行值交换后的values2:"+e.value2);
	}
	public static void main(String[] args) {
		exercise59 e = new exercise59();
		e.value1=10;
		e.value2=20;
		System.out.println("进行值交换前的value1:"+e.value1+" 进行值交换后的value2:"+e.value2);
		swap(e);
		System.out.println("执行swap方法过后的value1:"+e.value1+" 进行值交换后的value2:"+e.value2);
	}
}
//运行结果
//进行值交换前的value1:10 进行值交换后的value2:20
//在swap方法中进行值交换后的value1:20 进行值交换后的values2:10
//执行swap方法过后的value1:20 进行值交换后的value2:10

Analysis : Why this time successfully changed the value of it?

main stack area with swap stack area are cited exercise59 this class, but the main method first on exercise59 this class value1 with value2 initialize variables, and then swap the stack area again exercise59 this class value1 with value2 a value exchange.

A First Look at the memory map
Here Insert Picture Description
jvm in the end what did it?

The first step : create jvm main stack area, through new exercise59 (), this class will be loaded exercise59 go to heap memory, and allocate memory, this hexadecimal memory address, recorded in the main stack area, biography e to the variable, and the exercise59 this class were value1 and value2 parameter definitions. Value1 and value2 subsequently performed assignment, the changed parameter argument. Finally, the address of the object e pass parameters to swap () method.
Step Two : JVM stack creating the swap area, the definition of a parameter exercise59 e, to receive incoming exercise59 objects in the stack area. Successful reception, becomes a parameter argument. Then initialize variables temp, the value of value1 exercise59 this class assignment to temp, then exercise59 value of this class value2 is assigned to exercise59 this class value1, finally assign a value to temp exercise59 this class value2 , the stack area in the swap operation to complete the exchange. Everything will swap the stack area, from top to bottom are popping operation, swap stack area disappear.
The third step : main method is finished, the main area of the stack everything, from top to bottom are popping operation, main stack area disappear.

Analysis : In the swap () method in, value1 and value2 These two values are swapped successfully. Moreover, swap () method is finished, main () method in the value1 with value2 have been exchanged, it is very easy to create a false impression, what kind of illusion of it? Incoming swap () method is an object, rather than his duplicates. But in fact it, for example for instance, if I have a computer, I can have a variety of ways off, I can shut off the power directly in front of a computer, but I have chosen, I connect with this remote ssh ip address of the computer, I use the command to shut him up, e ip address is equivalent to objects in this case a reference to the address, I was with my current computer operation in the remote operation is the same as the referenced object is the same. So this is an absolute value of the transmission mode, e is only a reference variable, the system copy the e variable, but he did not copy exercise59 this class.

We can prove it swap () e with the main method () method e in the end is not the two variables!
In the last line we can swap () method added:

//将null赋值给e,让这个对象不再指向任何有效地址
e=null;
//运行结果:
//进行值交换前的value1:10 进行值交换后的value2:20
//在swap方法中进行值交换后的value1:20 进行值交换后的values2:10
//执行swap方法过后的value1:20 进行值交换后的value2:10

Analysis : e the object even when the swap () method points to null, value can be exchanged. This is because although the two variables e, but they point to the same memory address! That the operation is the same memory address!, The operation is carried out to transmit values!

Another glimpse of the memory map:

Here Insert Picture Description
Conclusion : java reference type parameters passed in the value is also transmitted.

Overall conclusion : java parameters passed in only one, i.e. the value transfer.

To wave, push it!
Group number: 781 121 386
group name: Life is short, I learned programming
welcome to join us, along with the exchange of technology !!!

Published 38 original articles · won praise 128 · views 10000 +

Guess you like

Origin blog.csdn.net/lujiangyang123/article/details/103294590