A basic question that interviewers love to ask: Is Java passed by value or by reference?
We all have our own opinions on this question, so what is the truth and how should we answer this question? Today we are going to analyze a wave
Value delivery? Passing by reference?
First, we need to know what value passing is and what reference passing is to understand how Java works. To understand these two transitions, you need to understand formal parameters and actual parameters
Formal parameters: The parameters used to define a function to receive the parameters passed by the function. For example, if we write a function, the parameters in the function are formal parameters
Public void test(String STR) {// STR is the formal argument system.out.println (STR); }Copy the code
Actual arguments: When we call a function, the arguments in parentheses after the function name are called actual arguments, as shown in the following example
public static void main(String[] args) { A a = new A(); a.test("chengxukong"); //chengxukong is an actual parameter}Copy the code
You can see that when you call a function with arguments, the actual arguments are passed to the formal arguments; Thus, the passing process has two cases, namely value passing and reference passing
Value pass is to give you the value of the parameter, and when you call a function, you pass a copy of the actual parameter to the function, so that the internal modification of the parameter does not affect the actual parameter; But the reference pass is different, it directly gives the actual address of the parameter to the calling function, the function can directly modify the address content, will affect the actual parameter
I, for example, we have A database A, only allows internal personnel operation, now have A project need to cooperate with other companies that the database data needs to be in the hands of A cooperation company, I can’t directly to our database A address to them, let them even database A us directly, if they walk the delete library, that is not to kill my boss I worship \
So at this time, copy our database table data to a new database B, partner companies can see this database B data, they can also operate at will, will not affect our database \
This operation is similar to value passing, if the partner company directly operates on our database, it is similar to reference passing, so you should know the difference
Java value passing or reference passing?
Now that we know the concepts of value passing and reference passing, what exactly is passing in Java? Let’s look at code analysis
public static void main(String[] args) {
Fans fans = new Fans();
int t = 1;
fans.test(t);
System.out.println("In main:" + t);
}
Copy the code
public void test(int t) {
t = 2;
System.out.println("In test:" + t);
}
Copy the code
In test: 2 In main: 1Copy the code
In main, t is set to 1 and in test, t is set to 2. The test function does not change the value of t passed in
\
If you pass in a reference object type, you must change the object’s content
int a = 10; String b = "fans"; public void test(Fans fans) { fans.a = 20; fans.b = "newFans"; Println ("In test, A: "+fans. A +", B: "+fans. B); }Copy the code
public static void main(String[] args) { Fans fa = new Fans(); fa.test(fa); Println ("In main, A: "+ fs.geta ()+", B: "+ fs.getb ()); }Copy the code
/ / outputCopy the code
In test, A: 20, B: newFans In main, A: 20, B: newFansCopy the code
The result is the same: the passed fans object is modified by test. \
\
So, base type is value pass, reference type is reference pass, right? If this is the case, let’s try again with the String reference type \
Public void test(String t) {t = ""; System.out.println("In test: "+ t); }Copy the code
public static void main(String[] args) { Fans fans = new Fans(); String tt = "program control "; fans.test(tt); System.out.println("In main: "+ t); }Copy the code
// Output In test: follow the public number In main: program controlCopy the code
The value of the passed parameter has not been changed
The above three examples show different results. What is the conclusion? Let’s analyze \
The first one we pass in is a primitive type, which refers to a value, and passing it is like an assignment, so it doesn’t affect the original value, like a=10, b=a, b=20, it doesn’t change a to 20; \
The second reference object is passed the value of the address that refers to type fans. Fa points to address 0x123456, so test points to address 0x123456. The function internally modifies the value of address 0x123456, which causes the external change
T = 0x123456; t = 0x123456; t = 0x123456; The key point is that t=” concern public account “; T =new String(” 连 接 号”); t=new String(” 连 接 号”); T points to another address, which says “public account”, so the original reference tt points to the original address 0x123456, unchanged
\
Some students may ask the question, why can the second one change the content of this address, but not the third one? Obviously, String is final and cannot be modified, whereas the second String can directly modify the contents of the address; So again, can we call it value passing? \
If you add fans = new fans () to the first line of the function, you can pass the value. If you change the position of the function argument, the output of the function outside and inside is different, and the inside of the function does not affect the outside of the function. If passed by reference, even if this sentence is added, the output should be the same inside and outside the function, and this also contradicts the notion that passing by reference changes the parameters passed in
thinking
Value passing and reference passing are not distinguished according to the content of passing, passing is not necessarily reference passing, according to the definition of the result to distinguish; \
In Java, value passing is used (remember, don’t get it wrong in your next interview)
In other ways to change the value of a reference type is by reference to change, when the transfer reference object, passing is the handle to the object reference copy, is replicated, which is in memory copy a handle to handle these two point to the same object, so you change the handle to the corresponding spatial data will affect the external variables
It is copied, but to the same address. If you point to a reference to another object, it does not change the value because you are using the copied handle \
Well, that’s all. I’m your learning and growth partner *
I hope that one day I can support myself by writing, and I am still honing my skills. This time may be many years. Thank you for being my original reader and communicator. Please believe that as long as you give me a love, I will eventually return you a page of love.
Thank you again for reading here, I will continue to update the technical articles and some records of the soul of life articles, if you feel good, feel something, please like, pay attention to, share three
Oh, right! The subsequent update of the article I will be put here in time, welcome to click to watch, are dry articles ah, suggest collection, at any time to check
Github.com/DayuMM2021/…