There have been different opinions on whether there is reference passing when calling functions in Java. Some people say that Java only has value transfer, while others say that Java has both value transfer and reference transfer. So, is there reference transfer in Java? Let me analyze it below.
Value transfer: It is a copy of the passed parameters. The modification of the parameters is only a modification of the copy. The function call ends, the copy is discarded, and the original variables remain unchanged (that is, the actual parameters remain unchanged)
Pass by reference: When parameters are passed to a function, no copy is made, but the parameters themselves are directly passed into the function. Any changes to the parameters within the function will be reflected in the original variables.
Both C++ and Java have the concept of reference, but they have completely different meanings in these two languages. In C++, we can use the form "int &b=a" to define a reference b of variable a. b is just an alias of a. b and a occupy the same storage unit in memory. Using the reference mechanism, we can call Functions implement two-way transfer of values - that is, transfer by reference. See the following code:
示例一 #include <iostream> using namespace std; int main() { void swap(int &,int &); int i=3,j=5; swap(i,j); cout<<"i="<<i<<"j="<<j<<endl; return 0; } void swap(int &a,int &b) { int temp; temp=a; a=b; b=temp; } 执行上面的程序输出的是i=5 j=3,a和b传递给swap()函数的时候,是传递的他们本身的地址,不是他们的拷贝,所以在函数中对他们的改变可以直接影响到实参a和b,这就是引用传递。 java中的引用更像C++中的指针,当我们定义一个对象时(比如Person p=new Person()),定义的对象实例会放到java堆中,而变量p(即引用)会放到java栈中,p指向堆中的Person对象实例。 为什么有很多人认为java有引用传递呢?一种情况是有人认为调用函数时其参数有可能是引用(如上面的p),所以java有引用传递,这部分人对引用传递根本没有正确的认识;而另一种情况看似有道理,但是仔细分析也是不正确的的,他们往往会用如下的代码来证明他们的观点: 实例二: 复制代码 class Demo{ int a; public Demo(int a){ this.a=a; } } public class TestQuote{ public static void main(String args[]){ Demo d1=new Demo(1); Demo d2=new Demo(2); System.out.println(d1.a); System.out.println(d2.a); function(d1,d2); System.out.println(d1.a); System.out.println(d2.a); } private static void function(Demo d1,Demo d2){ int a; a=d1.a; d1.a=d2.a; d2.a=a; } } 复制代码 他们的观点如下:执行上面的代码,调用function()函数以前输出的结果是1、2,调用function()函数之后输出的结果会是2、1,可见在函数内对d1和d2的改变反映到了原来的变量上,要不是不会输出2、1的。 这种解释是很迷惑人的,看上去好像很正确,下面的代码会很好的反驳上面的观点: 示例三: 复制代码 class Demo{ int a; public Demo(int a){ this.a=a; } } public class TestQuote{ public static void main(String args[]){ Demo d1=new Demo(1); Demo d2=new Demo(2); System.out.println(d1.a); System.out.println(d2.a); function(d1,d2); System.out.println(d1.a); System.out.println(d2.a); } private static void function(Demo d1,Demo d2){ Demo temp; temp=d1; d1=d2; d2=temp; } } 执行上面的代码,调用function()前后程序输出的都是1、2,此程序试图通过调用function()交换d1和d2,但是没有成功,为什么呢?因为d1和d2是值传递,function()中的d1和d2是main()函数中d1和d2的副本,调用完function()不会对main()中的变量产生影响。再看示例二中,function()函数内改变的并不是d1和d2本身的值,而是d1和d2指向的对象的值,调用完function()后d1和d2仍然指向函数调用前的堆地址,即函数参数是栈中的d1和d2,而不是堆中d1和d2指向的对象,即使你在函数中改变了堆中的对象,但没有改变函数参数的值。所以示例二并不是什么引用传递;可见java中只有值传递。 但是网上有很多针对"java值传递和引用传递进行的区别"的文章,如果读者看到的话一定要清楚,其中所说的引用传递是不正确的,他们所说的引用传递就是示例二中的那种情况。不幸的是,网上有很多文章都把示例二中的那样的传递看做引用传递,如果读者看到知道是什么意思就行了。
The above is the detailed content of Analyze whether there is reference transfer in java. For more information, please follow other related articles on the PHP Chinese website!

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...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment