在 Java 中通过引用传递? [英] Passing by reference in Java?

查看:53
本文介绍了在 Java 中通过引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C++中,如果需要修改2个对象,可以通过引用传递.你如何在java中完成这个?假设这 2 个对象是原始类型,例如 int.

解决方案

你不能.Java 不支持传递对变量的引用.一切都是按值传递的.

当然,当一个对象的引用通过值传递时,它会指向同一个对象,但是这不是通过引用调用.>

In C++, if you need to have 2 objects modified, you can pass by reference. How do you accomplish this in java? Assume the 2 objects are primitive types such as int.

解决方案

You can't. Java doesn't support passing references to variables. Everything is passed by value.

Of course, when a reference to an object is passed by value, it'll point to the same object, but this is not calling by reference.

这篇关于在 Java 中通过引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆