I can’t believe I’ve been using the Java programming language for so long and I’m using value passing. In this article we will walk you through the Java value passing feature.

preface

Does anyone think that when Java passes a parameter, if the parameter is a normal type, it is passed by value, and if it is an object, it is passed by reference?

It would be a mistake to think so.

Let’s take a look at Java’s value passing feature.

Java is really value passing

The best way to test for value passing is to swap methods, where we swap references to two objects to see if the original value is affected.

We can look at the following example:

  • I’m going to create two users: One is Joe, one is Joe.
  • We then call the identity swap method swap
  • In the identity swap method, confirm whether the identity swap is successful (you can see the result is successful).
  • In the case of passing by reference, the two users we created have swapped identities
  • But the actual result is two users
package com.zhj.interview; Public class Test15 {public static void main(String[] args) {User ZhangSan = new User(" ZhangSan "); User LiSi = new User(" LiSi "); System. The out. Println (" start exchange -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "); swap(ZhangSan, LiSi); System. Out. Println (" exchanging end -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "); System.out.println(" ZhangSan name: "+ zhangsan.name); System.out.println(" LiSi name: "+ lisi.name); } private static void swap(User ZhangSan, User LiSi){ User user; user = ZhangSan; ZhangSan = LiSi; LiSi = user; System.out.println(" ZhangSan name: "+ zhangsan.name); System.out.println(" LiSi name: "+ lisi.name); } } class User{ String name; User(String name) { this.name = name; }}Copy the code

Running results:

Began to exchange -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- after exchange ZhangSan name: li si after exchange LiSi's name: Zhang SAN exchange end -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- after exchange ZhangSan name: zhang SAN exchange after LiSi's name: li siCopy the code

Cause of illusion

The reason why we think that when Java passes a parameter, if it’s a normal type, it’s a value pass, and if it’s an object, it’s a reference pass is that when we modify the object we pass in, the content of the object we start creating changes.

The best way to think about it is:

The parameter we pass in is just the key. Passing a value is passing two keys to the method. Passing a reference is passing its own key to the method.

If we give a method our own key, our own key will be swapped when the key is exchanged within the method (pass-by);

If two extra keys are assigned to each other, the key exchanged in the method is the newly configured key, which will not affect our own key (value transfer).

It should be noted that, whether you use your own key, or a later key, open the door, modify the structure of the house, this change is not affected by the key, because the change is not the key.