10

So here is another worthy downvote question.

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

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?

Danrex
  • 1,657
  • 4
  • 31
  • 44
  • 2
    The additional complexity and confusion outweigh the benefits. – user2357112 Jun 08 '14 at 00:41
  • 1
    I don't think the question is "downvote worthy", but could you specify why you believe this is a disadvantage? – Guido Jun 08 '14 at 00:44
  • 2
    It isn't 'because I said so'. It is because Jim Gosling said so. – user207421 Jun 08 '14 at 00:46
  • Guido - I guess you lose some functionality by doing so? – Danrex Jun 08 '14 at 00:54
  • 1
    @Danrex note my answer which was hastily downvoted well before completion. – necromancer Jun 08 '14 at 00:55
  • 1
    It wasn't me Necro... I thought your answer was helpful – Danrex Jun 08 '14 at 00:57
  • 2
    Speculation: It's the simplest solution that could possibly work. Look at languages that allow either (like C#) how many times have you seen code that either uses th ref or out keywords? Almost none, and even when you do, you should almost always read it as "This function has more than one return parameter, and I couldn't be bothered to wrap it, also no anonymous classes or dynamic yet, or I don't understand the concepts", rather than "This method will do something useful and/or destructive to the object graph you provide". – Mikkel Løkke Jun 08 '14 at 01:08
  • Ok Mikkel thanks. So it's Java keeping a tight control on things it seems. – Danrex Jun 08 '14 at 01:10
  • 1
    posting the essence of my answer here as a comment: In sum, deeming a language as "call by value" or "call by reference" is not very meaningful. The correct characterization is, whether a language allows automatic copying of data to prevent modification by callee, and whether a language allows creating pointers to allow a callee to modify primitives. Java doesn't allow either, and C++ allows both. – necromancer Jun 08 '14 at 01:20
  • 3
    It's simpler than that. I think that for less advanced programmers the way Java does it is simplest to understand, when you're programming in C# and you see the `ref` or `out` keyword, you always need to figure out what it does. I have actually seen code that takes a parameter by reference and the first line of the method, just creates a new one. This creates an application which is next to impossible to debug (particularly if that object is a LINQ2SQL Entity), unless you want to manually step through all of it. – Mikkel Løkke Jun 08 '14 at 01:28
  • @MikkelLøkke replying here: that sounds like Java. The behaviors are: CALLER: Object p = new Object(); foo(p);, CALLEE1: void foo (Object p) { p.mutate(); }, CALLEE2: void foo (Object p) {p = new Object();}, CALLEE3 (C++ reference): void foo (Object & p) {p = new Object();} and possibly others. The differences are in automatic copying, automatic pointer creation and dereferencing, and there are enough differences in behavior that describing languages absolutely as "call-by-value" and "call-by-reference" is not very helpful. – necromancer Jun 08 '14 at 01:59
  • 2
    Additionally, the JVM implementation of method calls is dramatically simplified by using a stack-based pass-by-value model. – chrylis -cautiouslyoptimistic- Jun 08 '14 at 02:00
  • 1
    @necromancer There is no automatic copying, automatic pointer creation, or automatic dereferencing. Saying it's not very helpful is simply wrong. Here is a Fidle that demonstrates the difference: https://dotnetfiddle.net/Y5cGoo – Mikkel Løkke Jun 08 '14 at 03:28

1 Answers1

2

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.

rvijay007
  • 1,357
  • 12
  • 20
  • 4
    While this is the only answer which isn't factually wrong, it doesn't really address the OP's question which is *why*. – JBentley Jun 08 '14 at 00:48
  • 1
    The only thing you can't explisitly do is create the equivalent of ``int & x``, where in two separate places in the program can assign to the same *raw* ``int`` variable and have the changes propigate. You can of course get the same effect by placing the ``int`` into some container object and passing that object around, but you can't do it with just a base ``int`` (or ``double``, ``bool``, etc). – aruisdante Jun 08 '14 at 00:59