Looking at the public number on Java technology today, I saw a question about the transfer of values in Java, and the article discusses whether it is a value pass or a reference to a question that will be asked when a function is called in Java. have been exposed to similar problems before, but just know is the value of the pass, specific to why, has not been too clear. Today, I read it, I understand, write a blog record.
First, declare that the function argument in Java is a value pass, not a reference pass. Before you figure out the problem, you need to figure out what a value pass is and what is a reference pass.
Value delivery (pass by value): Refers to a copy of the actual parameter is passed to the function when the function is called, so that if the parameters are modified in the function, the actual parameters will not be affected.
Reference passing (pass by reference): means that the address of the actual parameter is passed directly to the function when the function is called, so that if the parameter is modified in the function, the actual argument is affected.
Please pay attention to my red marked words, very important. Believe that many people have an understanding of whether a value pass or a reference pass: When passing a base data type, is a value pass, which is a reference pass when the reference data type is passed. The reason for this is that code like the following is shown.
PackageCom.wuqi.p1; Public classValuepasstest { Public Static voidMain (string[] args) {intA = 1; //The basic data type is passed, because the value of a is passed to Param, so even if the pass function changes from//The value of the parameter, or the value of a is not changed. So we think that the value is passed when the basic data type is passedPass (a); System.out.println ("A=" +a); } Private Static voidPassintparam) {param= 2; System.out.println ("Param=" +param); }}
PackageCom.wuqi.p1;ImportCom.wuqi.p2.User; Public classPassTest2 { Public Static voidMain (string[] args) {User User=NewUser (); User.setname ("Wutianqi"); //The object is passed, because the reference user to the user is passed to Param.//Param.setname in the function will react to the real object. So we//think in this case is a reference to passPass (user); System.out.println ("My name is" +user.getname ()); } Private Static voidPass (User param) {param.setname ("Wuqi"); System.out.println ("My name is" +param.getname ()); } }
including myself, I used to think so. But none of us have noticed such a problem. Please look at the code
PackageCom.wuqi.p1; Public classPassTest3 { Public Static voidMain (string[] args) {String name= "Wutianqi"; //passing the string argument here, according to our previous view, here we should pass the name reference that will point to the string.//passed to Param, then modifying the value of the parameter in the PASS function directly affects the string to which the name reference points//, the result of the output should be my name is Wuqi my name is WuqiPass (name); System.out.println ("My name is" +name); } Private Static voidPass (String param) {param= "Wuqi"; System.out.println ("My name is" +param); }}
This code is based on the idea that our object is a reference, and the result of the output should be what the code says. But the real output actually does the following.
Hey, yo! What a situation! To subvert your perceptions? I was surprised to see this code! This shows that the previous understanding is wrong!
Don't panic! Let's take a look at the concept of value passing and reference passing. Here my red-flagged font worked. Reference passing is a direct pass of a reference, so modifying the parameters in the function will affect the actual parameters. According to this theory, there is no doubt, by the above example, that reference passing is wrong for Java function parameter passing. Looking at the concept of value passing, the value pass is a copy of the actual parameters, and the changes to the parameters do not affect the actual parameters. Pay attention to copy the two words!!! In the example above, if we think that we copied the name reference, we copied the value referenced by name and passed it to Param. Param= "Wuqi" is actually equivalent to Param=new String ("Wuqi"), when Param points to a new object. The actual parameter name still points to the original object. In this way the output and the correct results are on the right. This also proves that value is passed in Java rather than by reference.
It is important to remember that the object is passed a copy of the reference to the parameter.
Whether the Java Call Function pass parameter is a value pass or a reference pass