Method call by is a standard computer science term. Method calls are divided into value calls ( call by reference ) and reference calls ( call by value ) according to the parameter passing situation. There are many definitions of these two types of calls in the world. The most common saying is that the value is the value call, and the address is the reference call. This is actually very inappropriate. These statements can easily remind us that Java's object parameter passing is a reference call. In fact, Java's object parameter passing is still a value call.
Let's first use a piece of code to confirm why Java object parameter passing is a value call.
public class Employee { public String name=null; public Employee(String n){ this.name=n; } //Swap two Employee objects public static void swap(Employee e1,Employee e2){ Employee temp=e1; e1=e2; e2=temp; System.out.println(e1.name+" "+e2.name); //Print result: Li Si Zhang San} //Main function public static void main(String[] args) { Employee worker=new Employee("Zhang San"); Employee manager=new Employee("Li Si"); swap(worker,manager); System.out.println(worker.name+" "+manager.name); //The printing result is still: Zhang San Li Si} }The above result is very disappointing. Although the contents of the formal parameter objects e1 and e2 were exchanged, the actual parameter objects worker and manager did not exchange contents. The most important reason here is that the formal parameters e1 and e2 are the address copies of the actual parameters worker and manager.
As we all know, in Java, object variable names actually represent the address of the object in the heap (professional term is called object reference). When Java method is called, the parameters pass a reference to the object. Importantly, the memory addresses occupied by formal parameters and actual parameters are not the same. The contents in formal parameters are just a copy of the object reference stored in the actual parameters.
If you have some understanding of the local variable area of the Java stack in JVM memory management (see "Java Virtual Machine Architecture"), you will understand the above sentence very well. When the JVM runs the above program, running the main method and the swap method will push two memory spaces called stack frames in the Java stack. There is a piece of memory called the local variable area in the main stack frame to store references to the actual parameter objects worker and manager. The local variable area in the swap stack frame stores references to the formal parameter objects e1 and e2. Although the reference values of e1 and e2 are the same as those of worker and manager, they occupy different memory space. When the references between e1 and e2 are exchanged, the figure below clearly shows that it will not affect the reference values of the worker and manager at all.
Java object parameter passing is still a value call although the address (reference) is passed. It's time to give the reference call and value call an accurate definition.
Call by value: During the parameter passing process, formal parameters and actual parameters occupy two completely different memory spaces. The content stored by the formal parameter is a copy of the content stored by the actual parameter. In fact, the passing of Java objects meets this definition, except that the content stored by formal parameters and actual parameters is not the variable value in the conventional sense, but the address of the variable. Well, think back: Isn’t the address of a variable also a value?
Call by reference: During the process of parameter passing, formal parameters and actual parameters are completely the same memory space, and the two are not distinguished from each other. In fact, formal parameter names and actual parameter names are just different symbols in programming. During the program operation, the space stored in memory is the most important thing. Different variable names do not indicate the memory storage space occupied differently.
Generally speaking, the basics of the two calls are not whether the value or the address is passed (after all, the address is also a value), but whether the formal parameters and the actual parameters occupy the same memory space. In fact, pointer parameter passing in C/C++ is also a value call. If you don’t believe it, try the following C code!
#include<stdio.h> void swap(int *a1,int *b1){ int *t=a1; a1=b1; b1=t; } int main(){ int x1=100; int x2=200; int *a=&x1; int *b=&x2; printf("%d %d/n",*a,*b); swap(a,b); printf("%d %d/n",*a,*b); return 0; }But C/C++ has reference calls, which is a variable declaration method called reference int a; int &ra=a; where ra is an alias of a. There is no difference between the two in memory and occupy the same memory space. Passing parameters through reference (alias) is in line with the characteristics of reference calls. You can try the running results of void swap(int &a1,int &b1);
Through this article, you should know whether Java method parameters are called reference or value.