2021/03/21 Problems encountered when passing parameters to methods in Java

Introduce

Our purpose is to exchange the contents of the elements in the array. Below is the code in Figure 1, which is the code I wrote for the first time.

Code Figure 1

Insert picture description here

  • The parameter list of the exchange method we defined is formal parameter a and formal parameter b
  • The result of this program is still 2 1. That is, the content of the elements in the array has not changed.
  • why? Didn't we pass in the values ​​of the two array elements?
  • Please look at the memory diagram 1 below

Memory map 1

Insert picture description here

  • Illustration: Since we don't have the address of the array when we call the exchange method, even if data exchange occurs, it will not affect the original array.
  • Let’s talk about our code Figure 2 below

Code Figure 2

Insert picture description here

  • What is the difference between code diagram 2 and code diagram 1?
  • The main difference is that the parameter list of the exchange method is different
  • The parameter list of the exchange method in code Figure 1 is the formal parameter a and the formal parameter b
  • The exchange method parameter list in code Figure 2 not only has a formal parameter a and a formal parameter b, we also have an additional parameter group
  • So when we use the exchange method, we can directly change the contents of the array
  • The result of running the program this time is 1 2
  • Please see memory diagram 2

Memory map 2

Insert picture description here
When we call the exchange method, the address can point to the array stored in the heap.

expand

  • First, we first introduce two concepts, value passing and reference passing
  • Value transfer: Pass the actual parameter to the method, and the actual parameter copy is handed over to the formal parameter. No matter how the formal parameter changes the copied actual parameter, it will not affect the actual parameter.
  • Pass by reference: Pass the actual parameter to the method, and the actual parameter is directly handed over to the formal parameter. If the formal parameter is changed, the actual parameter will be affected accordingly.
  • Are our above code diagram 1 and code diagram 2 passed by value or by reference?
  • Some people think that code figure 1 is value transfer, and code figure 2 is reference transfer. Because the parameters passed in in Code 1 are all numeric values, the parameters passed in in Code Figure 2 have numeric values ​​and addresses.
  • This idea is wrong.
  • There is no pass by reference in Java, all are passed by value
  • Code Figure 1 has no address and cannot affect the elements in the array. The actual parameters are index values ​​0 and 1. After we use these two actual parameters, the two actual parameters have not changed, but 0 and 1
  • Code Figure 2 has an address, which can be used to affect the elements of the array. The actual parameter is the array address value, the index values ​​are 0 and 1. After we use these three actual parameters, the three actual parameters have not changed, the array address is still the address of the array, and the index values ​​are still 0 and 1.
  • Java uses a special method to achieve the same effect as the reference pass, but it will not affect the actual parameters. This type of pass is called shared pass. Shared transfer is a special case of value transfer.
  • Let us introduce the third situation, please look at code diagram 3 and code diagram 4.

Code Figure 3 User Class and Code Figure 4 Test Class

Code Figure 3 User class

Insert picture description here

Code Figure 4 Test class

Insert picture description here

Program running results

Insert picture description here

  • Why is this result? We passed in the address value of a user object. It is said that the print output in the main method should be affected by the method method, and the result of both times should be the "method method". But why is it different from what we expected?
  • Let's explain this problem, please see memory diagram 3

Memory Figure 3

Insert picture description here

  • Remarks: When we call the method method, a new user object is created in the method body. Therefore, the user object pointed to by the method method and the main method is not the same

Guess you like

Origin blog.csdn.net/hypertext123/article/details/115067730