gpt4 book ai didi

java - 共享调用和引用调用是否仅在多线程时有所不同?

转载 作者:搜寻专家 更新时间:2023-10-31 08:23:26 25 4
gpt4 key购买 nike

如果使用Call-by-Reference 调用函数,则对函数内变量所做的任何更改都会立即影响调用者。对于 Call-by-Sharing,它在函数结束时受到影响。

问题 1:Java 是否使用Call-by-Sharing 而不是 Call-by-Reference

问题 2:我认为 Call-by-Sharing 不同于 Call-by-Reference only而多线程。创建它只是为了减少在其他线程中使用时并发覆盖值;提供一致性。我说得对吗?

最佳答案

我建议您不要使用“共享调用”术语。作为this Wikipedia article状态:

"However, the term "call by sharing" is not in common use; the terminology is inconsistent across different sources. For example, in the Java community, they say that Java is pass-by-value, whereas in the Ruby community, they say that Ruby is pass-by-reference[citation needed], even though the two languages exhibit the same semantics. Call-by-sharing implies that values in the language are based on objects rather than primitive types."

"Although this term has widespread usage in the Python community, identical semantics in other languages such as Java and Visual Basic are often described as call-by-value, where the value is implied to be a reference to the object."

底线是 Java 使用“通过共享调用”......但他们不这么调用它,如果你想让 Java 人理解你,你可能也不应该这样称呼。


I think that Call-by-Sharing differs from Call-by-Reference only while multithreading. It is created only to decrease concurrent over-writing of values while it is being used in some other thread; to provide consistency. Am I right?

不,你不对。

在值是对象引用的情况下,“共享调用”的真正含义是“按值调用”。真正的“通过引用调用”意味着您(实际上)正在传递一个变量的地址,并且被调用的方法可以更新该变量。

关于java - 共享调用和引用调用是否仅在多线程时有所不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13899171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com