为什么不支持java通过引用像C ++ [英] Why doesn't java support pass by reference like C++

查看:95
本文介绍了为什么不支持java通过引用像C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读取每个原始数据类型和对象引用传递的值?

I have read every where that primitive datatype and object references are passed by value?

我试图在Google中搜索为什么不支持java通过引用,但我只能获得java不支持通过引用我找不到任何原因

I have tried searching in Google why doesn't java support pass by reference , but I only get java does not support pass by reference and I couldn't find any reason behind it.

为什么你不能通过引用传递原始数据类型?

Why can't you pass primitive datatype by reference ?

编辑:大多数人都已经关闭了我的问题,

Edit : Most of the people have closed my question assuming that it is subjective and argumentative.

这不是,它有一个明确的答案,我的问题是为什么不能创建一个抽象类的对象,它不是重复因为大部分的回答只是简单地说了。

Well it is not, it has a definite answer, my question is like why can't you create a object of abstract class and it is not duplicate as well because most of the answer just plainly say NO.

谢谢。

推荐答案

p>按照设计:


有些人会错误地说,
对象是通过引用传递的。在
编程语言设计中,术语
通过引用正确地表示
当一个参数传递给
函数时,被调用的函数获得一个
引用原值,而不是
的值的副本。如果函数
修改其参数,
中的值调用代码将更改
,因为参数和参数使用
在内存中的同一个槽.... Java
编程语言不通过引用传递
对象;它传递对象
引用的值。因为同一引用的两个
副本引用
相同的实际对象,通过一个引用变量所做的
的更改是通过另一个引用变量可见的
。有
正好一个参数传递模式 -
通过值 - 并且有助于保持
的东西简单。
- James Gosling,等人,The Java Programming Language,第4版

Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory.... The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple. -- James Gosling, et al., The Java Programming Language, 4th Edition

至于更深层的原因, take:它是两个事实的组合:

As for deeper reasons, here's my take: it's the combination of two facts:


  1. Gosling引文的最后一行:...有助于保持简单。 。

  2. 与C ++不同,Java是使用堆上分配的所有对象进行垃圾回收。

我不能帮助,如果你不喜欢第一个。你必须告诉詹姆斯·戈斯林和比尔·乔伊和所有其他设计Java的人,他们犯了一个关键的错误。祝你好运。 Java通过几种措施比C ++更广泛地使用。

I can't help it if you don't like the first one. You'll have to tell James Gosling and Bill Joy and all the other folks who designed Java that they made a critical error. Good luck with that. Java is far more widely used than C++ today by several measures. The marketplace, however imperfect, has not penalized Java for what you perceive as an oversight.

在C ++中传递值会给开发者带来负担(例如分配和分配的要求)复制构造函数)和编译器编写器(例如区分堆栈和堆变量,通过值传递和使用const和非const的引用的所有排列)。

Pass by value in C++ places burdens on both the developer (e.g. requirement of assignment and copy constructors) and the compiler writer (e.g. differentiating between stack and heap variables, all permutations of pass by value and reference with const and non-const).

除了设计师的品味外,还有更多的技术解释。我不是垃圾收集系统的设计和实现的专家,但也许这影响了他们的选择,由于我不知道的技术原因。

The second one might have more of a technical explanation besides the designers' taste. I'm not an expert in the design and implementation of garbage collected systems, but perhaps that influenced their choice for a technical reason that I don't know.

这篇关于为什么不支持java通过引用像C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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