Whether the Java function parameter is passed is a value or a reference is really confusing. And many times because of the ambiguity of this problem even caused some mistakes. The most common argument is that the base type is a reference to the value, the object passed. As for the basic types, we all agree that there is nothing to argue about. But for object passing, there is also a value pass. Let's start with an example. A simple class person, with only one attribute name, and one personnel system. As shown below:
Public class person{ private String name; Public persion (String name) { this. Name = name; } Public String GetName () { returnthis. Name; } Public void setName (String name) { this. Name = name; }}
Public classpersonnalsystem{ Public Static voidchangeName1 (persion person) {Person.setname ("Bob"); System.out.println ("Person name in ChangeName1:" Person.getname ());//Bob, it's been modified. } Public Static voidchangeName2 (persion person) {person Anotherperson=NewPersion ("Tom"); person=Anotherperson; System.out.println ("Person name in ChangeName2:" Person.getname ());//Tom has been modified. } Public Static voidMain (string[] agrs) { person person=NewPersion ("Lily"); System.out.println ("Before change name of person ' s name is" +person.getname);//LilychangeName2 (person); System.out.println ("After first change name person's name is" +person.getname);//Modified by BobchangeName2 (person); System.out.println ("After second change name of person ' s name is" +person.getname);//It's still bob, not modified. }}
Running this program can be seen, changeName1 modified the name of person, indeed is to pass the reference. However, when the person is passed to changeName2, although it is successful within the method, Bob is still not modified in the main function. It seems to be a passing value again. This is because the object is passed through an object's address, not a reference to the object, or a copy of the object. Assuming that the first person's address is 60,changename1, the parameter received is address 60, so the modification is made at address 60, so the object changes. The address of the new Anotherperson in ChangeName2 may be 65, But it's not 60. When assigning a anotherperson to a person, only 60 of the person's code is changed to 65, and subsequent modifications are performed at 65, thus having no effect on the person in Main (60).
So in a word, I think the Java parameter passing process is a value pass.
- Basic type pass-through value
- A reference type passes a value that is simply an object address value, not an object or a copy.
This is also true for String,integer, double, and other immutable types. Because these objects are immutable, each pass is a value. of course, if you do not want to think that the object is a reference to pass, but the first to distinguish the immutable object string, and then the doctrine of changeName2 in this case thousand hope that the original object is changed.
The parameter of the Java function is said