15

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 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.

Thanks.

Searock
  • 6,278
  • 11
  • 62
  • 98
  • 2
    possible duplicate of [Are Java function parameters always passed-by-value?](http://stackoverflow.com/questions/1110826/are-java-function-parameters-always-passed-by-value) – duffymo Mar 14 '11 at 12:15
  • Because it's been asked and answered many times in many places, including Stack Overflow. You didn't read or think much if you couldn't find a reason. – duffymo Mar 14 '11 at 12:18
  • 3
    @duffymo This is not a duplicate...OP knows you can't and is asking *why* you can't. I don't understand why this deserves to be closed...even if the reasons Java did this are subjective, stating that the reasons were subjective would be a valid answer. – Michael McGowan Mar 14 '11 at 12:19
  • @duffymo I know it is not supported but I want to know why it is not supported. The link you have provided does not provide my answer. – Searock Mar 14 '11 at 12:25
  • 4
    The jvm works simple, on a method invocation it copies the arguments as they are on the stack. Since primitives are on the stack it will only copy the value, the same happens for references. The jvm could support a by reference passing of these values by passing stack offsets instead of copying but that would make the language and therefore the compiler, implementation, reflection, optimisation and a large number of other things much more complex.________ So the question is: Is pass by reference worth the pain? Apparently the language designers thought that it is not. – josefx Mar 14 '11 at 12:26
  • 2
    @duffymo - this question may be theoretical, but certainly not subjective and not a duplicate that I can see. – Nir Levy Mar 14 '11 at 12:35
  • 2
    This is clearly not subjective. Furthermore there is a possible helpful answer that could be given, explaining how you can in fact work around this limitation quite easily, so it's hardly a limitation at all. So the decision to close it was pretty thoughtless, and it should be reopened. – Daniel Earwicker Mar 14 '11 at 13:03
  • 2
    All right, you've convinced me. I'll vote to reopen. – duffymo Mar 14 '11 at 13:13
  • No one knows why inertia works the way it does, either. If you're asked that in an interview, there'd be nothing wrong with explaining as best you knew how and then asking to hear the interviewer's answer. I wouldn't be surprised to find out that the interviewer didn't know a satisfactory deeper reason. – duffymo Mar 14 '11 at 13:57
  • @josefx - your answer makes sense to me – Santosh Tiwari Feb 24 '12 at 21:41

2 Answers2

15

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 3
    OP seems to know that it was by design...it seems he is specifically asking for those deeper reasons. – Michael McGowan Mar 14 '11 at 12:22
  • @Michael McGowan Thanks, at least there is one person who understands my question. – Searock Mar 14 '11 at 12:31
  • Better contact James Gosling - perhaps he can explain the deeper reason. – duffymo Mar 14 '11 at 13:02
  • 1
    @duffymo - or James Gosling can post an answer? – Daniel Earwicker Mar 14 '11 at 13:04
  • @duffymo I am not saying that java has done some thing wrong. My question was simple that was there any drawback? Like java does not provide support for multiple class inheritance for the sake of garbage collector. No offense : ) – Searock Mar 14 '11 at 13:17
  • Java didn't go with single inheritance for the sake of GC; it was simplicity and the difficult experience that some people had with multiple inheritance in C++. – duffymo Mar 14 '11 at 13:25
  • 1
    *"You'll have to tell James Gosling and Bill Joy and all the other folks who designed Java that they made a critical error.*" Why? I'm not seeing anyone claim it was a bad design choice. – arkon May 23 '16 at 16:39
6

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...

duffymo
  • 305,152
  • 44
  • 369
  • 561
Nir Levy
  • 4,613
  • 2
  • 34
  • 47
  • So you're saying that C++ is not secure because it allows PBR? Interesting. – duffymo Mar 14 '11 at 14:20
  • Maybe the word "dangerous" would be more appropriate, but yes, imho C++ is much more dangerous then Java (and not just because of PBR). I'm not the best C++ programmer but all that pointer and memory management is not the safest way to program (it is, non the less, much more flexible and faster at that). But then, we are sliding into the "subjective" zone we all hate... – Nir Levy Mar 14 '11 at 20:40
  • 1
    Lots of C++ code out there. I'm not aware that anyone has died because of new and delete calls. I think it's overstating the dangers. – duffymo Mar 17 '11 at 16:09
  • 5
    I know this post is not about C#, but I think it's worth mentioning. C# requires the argument to have "ref" at the beginning of the variable when invoking a method with pass-by-reference parameter. If omitted, there will be an error, this will inform the user of the method that the parameter is to be passed by reference, thus making it secure. I hope Java will do the same. – kazinix Sep 16 '12 at 10:35