I read something online that incorrectly stated that standard int [], etc arrays in Java were passed as copies, rather than passing references to the arrays, in analogy with the basic numerical types, and ended up overwriting an array when I thought I was modifying a copy. Can I chalk this up as a design choice to make things simpler to the target audience for Java circa mid-90s? (making objects look the same syntactically as C arrays, or are arrays really not of type “Object” in Java?)
That is, why didn’t they just do something like:
Array array = new Array(<size>);
Additionally, why didn’t they make everything (except literals) pass-by-reference to ensure consistency? (ints would then be passed as references to the int, not as the value of the int, so modifying a variable that’s an argument of a method within that method would modify the value of the original variable, etc.)
Link to a discussion of pass-by-reference vs. pass-by-value in Java
Yeah sure. There is no denying that a memory address is also a value. So pass-by-reference really is just a special case of pass-by-value.
+1 for the most Filistine answers ever on SO.
BTW, I think I might also have a portion of an answer to
“why didn’t they just make everything pass-by-reference to ensure consistent behaviour?”
In that case, how does the system deal with invocations where the arguments are literals, say ‘result = f(2);’ ?
Must that literal then not also be passed by reference ? And if that is so, would not that open up to the possibility of having your literals changing value by some invocation that updates the reference ? In which case the term ‘literal’ might become somewhat inappropriate, and a lot of possible code optimizations become impossible for the compiler to implement ?