4 ms·
Java is Pass-by-Value, Dammit
- Edmond 16y agoGood post.... I am a Java developer with almost 10 yrs of experience and I know I often make that mistake even on Job interviews:).... The problem is that people used to Java tend to think more about the intuitive consequence of passing by ref vs value. When a Java dev thinks of passing by reference he's thinking: "I can alter the state of the reference and the changes would be visible globally, including from within the calling context". In other words Java devs think about altering arguments in terms of altering their state as opposed to altering the reference which is just a numeric address to a memory location. People who program in languages lacking object/class-like structures are more aware of the need to use parameters both as arguments and return values, something that is foreigner to Java programmers. I haven't done any serious C++ programming in years and I have to admit the examples you used to demonstrate your point; ie modifying the value of arguments appears very strange to me now, I obviously did a lot of that when I did C/C++ development but not having done it in such a long time it really does seem like an unusually way of getting return values:)
- SeanLuke 16y agoJava is in good company. Lisp is also pass by value.
- runT1ME 16y agoAlmost EVERYONE I've interviewed, including senior programmers, think that if you do this: void nullThis(Object o) { o = null; } Object o = new Object; nullThis(o); System.out.println(o.toString()); throws a NPE
- binaryfinery 16y agoPassing an object by reference is the same thing as passing an object reference by value. It is just semantics. Its not even splitting hairs. Its demanding that his specific definitions are somehow more correct than any other.
- okmjuhb 16y agoThis is not at all correct because passing by reference and value have defined and distinct meanings. Consider the difference between C++, which has actual pass by reference: void swap(int& x, int& y) { int t = x; x = y; y = t; }, and Java: void swap(Integer x, Integer y) { Integer t = x, x = y; y = t; }. The C++ version will result in changes to the variables in the calling function and the Java version will not. The real problem is that Java created needless confusion by calling its pointers references for purely marketing reasons.
- binaryfinery 16y agoNo the real problem is that you fail to understand that Java gets to call these things what ever it likes because language has context. And your example fails to compare apples to apples. In Java Integer is an object, but int is not. You are confusing Java things with C++ things. As you can see, Java is not C++, and so your C++ terms and definitions do not apply here. Thank you for playing. Move along.
- okmjuhb 16y ago"No the real problem is that you fail to understand that Java gets to call these things what ever it likes because language has context." This is silly; "pointer" and "reference" both had meanings before Java came along. If Java called pointers "names" instead of "references" it wouldn't mean that its function call semantics were pass-by-name either. And besides; if your argument is that we should use the Java terminology to describe all the aspects of Java under discussion, the fact that the all the Java designers make explicit that the function call semantics pass references as values, not objects as values. "And your example fails to compare apples to apples. In Java Integer is an object, but int is not. You are confusing Java things with C++ things." The argument applies if you use int in place of Integer just the same; or if you make a C++ Integer wrapper class analogous to the Java one. Any comparison here will of course be apples to oranges because C++ supports pass by reference and Java does not. The swap function is impossible to write in Java because it requires allowing called functions to change the values viewed in the caller, which is almost the definition of passing arguments by value. "As you can see, Java is not C++, and so your C++ terms and definitions do not apply here. Thank you for playing. Move along." Leaving aside the childish and insulting tone, the definition of "pass by reference" has nothing to do with C++; if the code were in perl it would still be pass by reference, because "pass by reference" has a meaning that exists outside of any particular programming language and describes a concept. Java function calls are not part of that concept; "there is exactly one parameter passing mode in Java - pass by value".