Home > Java > javaTutorial > body text

Are Java method parameters called by reference or by value?

高洛峰
Release: 2017-01-22 10:59:53
Original
1534 people have browsed it

Method call (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 parameters passed. There are many definitions of these two types of calls in the world. The most common saying is that the one that passes the value is a value call, and the one that passes the address is a reference call. This is actually very inappropriate. These statements 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.

We first use a piece of code to confirm why Java's object parameter passing is a value call.

public class Employee {
 
  public String name=null;
 
  public Employee(String n){
    this.name=n;
  }
  //将两个Employee对象交换
  public static void swap(Employee e1,Employee e2){
    Employee temp=e1;
    e1=e2;
    e2=temp;
        System.out.println(e1.name+" "+e2.name); //打印结果:李四 张三
  }
  //主函数
  public static void main(String[] args) {
    Employee worker=new Employee("张三");
    Employee manager=new Employee("李四");
    swap(worker,manager);
    System.out.println(worker.name+" "+manager.name); //打印结果仍然是: 张三 李四
  }
}
Copy after login

The above result is very disappointing. Although the contents of formal parameter objects e1 and e2 are exchanged, the actual parameter objects worker and manager are not exchanged. content. The most important reason here is that the formal parameters e1 and e2 are address copies of the actual parameters worker and manager.

As we all know, the object variable name in Java actually represents the address of the object in the heap (the technical term is called object reference). When a Java method is called, the parameter passed is a reference to the object. The important thing is that the memory addresses occupied by formal parameters and actual parameters are not the same. The content in the formal parameters is just a copy of the object reference stored in the actual parameters.

If you know something about 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 respectively, they occupy different memory spaces. When the references of e1 and e2 are exchanged, the figure below clearly shows that the reference values ​​of the worker and manager will not be affected at all.

Are Java method parameters called by reference or by value?

#Although Java object parameter passing passes an address (reference), it is still a value call. It's time to give a precise definition of call by reference and call by value.

Call by value: During the parameter transfer process, formal parameters and actual parameters occupy two completely different memory spaces. The content stored in the formal parameter is a copy of the content stored in the actual parameter. In fact, the transfer of Java objects conforms to this definition, except that the content stored in the formal parameters and actual parameters is not the variable value in the conventional sense, but the address of the variable. Cough, think back and think about it: isn’t the address of a variable also a value?

Call by reference: In the process of parameter transfer, the formal parameters and actual parameters are completely in the same memory space, and they are not separated from each other. In fact, formal parameter names and actual parameter names are just different symbols in programming. During the running of the program, the storage space in the memory is the most important. Different variable names do not mean that the memory storage space occupied is different.

Generally speaking, the fundamental difference between the two calls is not whether the value or the address is passed (after all, the address is also a value), but whether the formal parameters and 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;
}
Copy after login

But C/C++ is called by reference. This is a variable declaration method called reference in C/C++: int a; int &ra=a; where ra is a. Alias, there is no difference between the two in memory and occupy the same memory space. Parameter passing by reference (alias) conforms to the characteristics of reference calling. You can try the operation results of void swap(int &a1,int &b1);.

Through this article, you should know whether Java method parameters are called by reference or by value.

For more articles related to whether Java method parameters are reference calls or value calls, please pay attention to the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template