为什么Java仅按值传递? [英] Why is Java pass by value only?

查看:163
本文介绍了为什么Java仅按值传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是另一个有价值的downvote问题。

So here is another worthy downvote question.

我知道Java IS是按值传递的,这意味着什么以及它是如何工作的。所以这不是另一个你可以解释什么是价值传递。我更为好奇的是,为什么Java不包括通过引用传递?我想这会有用吗?知道如何巩固我头脑中的推理也是有帮助的。

I understand that Java IS pass by value and what this means and how it works. So this is not another can you explain what pass by value is. I am more curious as to WHY Java does not include pass by reference? I would imagine this would be useful? It would also be helpful to know to cement the reasoning in my head....

我讨厌'这是因为'场景肯定相当于'因为我我说过了'。那么有没有人能够回答为什么Java只包含值传递?

I hate 'it is because it is' scenarios surely the equivalent of 'because I said so'. So does anyone have an answer as to why Java only includes pass by value?

推荐答案

David Flanagan的O'Reilly的Jutshell中的Java说得最好:Java通过引用来操纵对象,但它通过值将对象引用传递给方法。'这是Java的设计决策。当您传递对象时,您仍在操纵相同的底层对象,因为它们都引用相同的内存位置。所以我不确定您正在考虑的具体情况是您无法使用现有的Java机制。

O'Reilly's Java in a Nutshell by David Flanagan puts it best: "Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'" This was a design decision by Java. When you pass objects around, you are still manipulating the same underlying object as they all reference the same memory location. So I'm not sure what specific scenario you are thinking about that you can't do with the existing Java mechanisms.

这篇关于为什么Java仅按值传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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