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); //打印结果仍然是: 张三 李四 } }
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.
#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; }
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!

Cloud computing significantly improves Java's platform independence. 1) Java code is compiled into bytecode and executed by the JVM on different operating systems to ensure cross-platform operation. 2) Use Docker and Kubernetes to deploy Java applications to improve portability and scalability.

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

Containerization technologies such as Docker enhance rather than replace Java's platform independence. 1) Ensure consistency across environments, 2) Manage dependencies, including specific JVM versions, 3) Simplify the deployment process to make Java applications more adaptable and manageable.

JRE is the environment in which Java applications run, and its function is to enable Java programs to run on different operating systems without recompiling. The working principle of JRE includes JVM executing bytecode, class library provides predefined classes and methods, configuration files and resource files to set up the running environment.

JVM ensures efficient Java programs run through automatic memory management and garbage collection. 1) Memory allocation: Allocate memory in the heap for new objects. 2) Reference count: Track object references and detect garbage. 3) Garbage recycling: Use the tag-clear, tag-tidy or copy algorithm to recycle objects that are no longer referenced.

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor