开发者

Why doesn't java support pass by reference like C++

开发者 https://www.devze.com 2023-02-16 13:05 出处:网络
I have read every where that primitive datatype and object references are passed by value? I have tried searching in Google why doesn\'t java support pass by reference , but I only get java does not

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

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 defini开发者_运维知识库te 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.

Thanks.


By design:

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

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

  1. The last line of the Gosling citation: "...that helps keep things simple..."
  2. Unlike C++, Java is garbage collected with all objects allocated on the heap.

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.

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.


My understanding is that the reason for not having pass-by-reference is mostly for security reasons: passing things by reference would enable a function to change stuff that is outside its scope and that means that my object (reference) may be replaced if I call a malicious function.

To elaborate: In Java, encapsulation is important and a safety measure. When giving a (pointer) to an object to a function that someone else wrote, I (as the caller) should be convinced that the function I call can only do with that object what I allowed it to do (using public members). Allowing a PBR would leave me (as the caller) in an unknown state after the function finished as I would not know if I am handling my own object or something else...

0

精彩评论

暂无评论...
验证码 换一张
取 消