7 ms·
Huge war over whether Java is pass by reference or pass by value.
- jonsen 16y agoThese kind of discussions is a symptom. Too few programmers have a clear mental picture of what goes on below their programming language.
- yock 16y agoI think also they don't have a clear understanding of what each term really means.
- 16s 16y agoIn C++, pass-by-reference means the method gets a reference to the thing. Pass-by-value means the method gets a copy of the thing. Do these terms mean something different in Java? Again, I'm not a big Java dev, but I am interested in why there's such a big debate on this topic. In C++, I typically pass-by-reference when I need to change the thing. When I don't need to change the thing (but it's expensive to copy) I pass by const reference so the thing cannot be changed, but is still efficient. I seldom pass-by-value unless it's a primitive type (int, char, etc.).
- awj 16y agoIt's also a symptom of zooming way in on one context and ignoring another, or just general annoying pedantry. From the perspective of an object being passed to a method, Java is pass by reference. No copy of the object is made. From the perspective of the implementation of methods, Java is pass by value, with the "value" representing any "object" involved being a pointer to that object. Most of this argument seems to be people choosing one view of this duality and screaming that the other side is stupid and wrong. It's a tedious and annoying thing to see play out (although nowhere near as bad as your average "static vs dynamic typing" debate).
- bena 16y agoI think the difference is somewhat more important than you give it credit for. If Java was truly pass-by-reference, then certain things that are not currently possible would be. It would make some existing code pretty silly frankly.
- awj 16y agoDo you have any good examples? Because I never could come up with a good one. Probably 99% of the reference usage I've seen (all in C++, fwiw) could be about as easily accomplished with pass-by-sharing through pointers. The actual valid uses never seemed worth the nasty copy constructor bugs created by people mistaking reference semantics for pointer semantics. At this point I gave up on "true" references so long ago that I tend to fall in with everyone else and call pointer-based sharing "pass by reference". I suppose I should amend my initial comment to specify that most of the team "pass by reference" is really "pass by sharing (through pointers)", but I'll wait on that until something convinces me that true references shouldn't go the way of dynamic scoping.
- qw 16y agoWhat about primitive variables, like "int"? If Java passed the variable reference (and not a copy), we should be able to modify it
- awj 16y agoYes, if it did that we would be able to modify it (and have those modifications reflected outside function scope). It doesn't, so we aren't. Primitives add complexity to many aspects of Java, calling semantics being one of them. I'd rather they'd went the route of forcing you to always use wrapped primitive types, but it's hard to fault the Java designers on that decision, especially when you consider the context of the time when it was made.
- fleitz 16y agoWeird. It's pass by ref for objects and pass by value for primitives. Why the flame war?
- randrews 16y agoIt's a war over semantics. Everyone agrees that primitives are pass-by-value, but when you pass an object, is that pass-by-reference because you're passing a reference to the object, or pass-by-value because you're passing the reference by value? It's like the Java equivalent of airplane-on-a-treadmill.
- panic 16y agoThere isn't any semantic ambiguity. The phrase "pass by reference" has a specific meaning. Java does not support pass by reference. Here's a quick test to see if your language uses pass by reference (when translated into Java, it will output "pass by value"): function f(x, y) { x = y } a = new A() b = new A() assert(a != b) f(a, b) if (a == b) { print('pass by reference') } else { print('pass by value') }
- randrews 16y agoYes, you're right, but the argument is still about semantics. Java programmers don't think in terms of passing a reference, they think in terms of passing an object (which happens to be done by passing a reference to it). There's a correct answer to airplane-on-a-treadmill too, but that doesn't mean it's not an argument about semantics. Please let's just stop this here before this thread becomes like that other thread.
- nopassrecover 16y agoAssumes equivalence is referential equivalence, and not overwritten. Of note, in some languages where there are immutable strings (e.g. java I believe) there is only "one true string" for each string (of course this isn't directly relevant to the example code given above). This means "var a = "test"; var b ="test"; assert(a==b);" holds true for referential equality.
- philbarr 16y agoI get a sick kick out of telling devs new to Java this when they run across it: "Java is ALWAYS pass-by-value...", they nod, "...including OBJECT REFERENCES." "What?"
- rbanffy 16y agoMy faith in mankind dies a little with every Java project we kick off... And people ask why...
- m_myers 16y agoHere's a good explanation (or several, actually) on Stack Overflow: http://stackoverflow.com/questions/40480/is-java-pass-by-reference http://stackoverflow.com/questions/40480/is-java-pass-by-ref...
- deleted 16y ago[deleted]
- martinkallstrom 16y agoI'm the original designer for TheServerSide.com and .net logotypes. Originally the entire websites, but only the logotypes and headers remain. They're over a decade old, I'm getting nostalgic whenever I see them. It is amazing that the logotypes still survive. Sorry for OT.
- DjDarkman 16y agoIt's pass by value for primitives and pass by reference for objects. But it depends on what you call a reference. If all java objects would be immutable, you could say it's pure pass by value, but since you can alter the original object's state in the method, I call it pass by reference. Before downvote: please explain, thank you.
- awj 16y ago...except that when methods are implemented a pointer to the "object" is what gets handed over. The narrow distinction that this pointer is passed by value makes some people feel exceedingly clever, despite the fact that the semantics of pass-by-reference in respect to the object itself are preserved.
- stonemetal 16y agoGet a metaphorical microscope and focus in on call semantics in Java. Arguments no matter what their type are pass by value. So then What are you allowed to pass as arguments to a function in java? Primitive types, it is impossible to pass an object to a function in Java. Then what actually happens when people claim they are passing an object to a function in Java? It is passing a primitive type of type object reference to the function. It is doing so by value.
- wlievens 16y agoYou're completely wrong, sorry. Pass by reference means you change the value of a variable passed from the caller's context. You can't do that in Java.
- deleted 16y ago[deleted]
- bokchoi 16y agohttp://blog.tmorris.net/java-is-pass-by-value/ http://blog.tmorris.net/java-is-pass-by-value/
- kevinpet 16y agoJava is pass by value with mutable objects. Pass by reference is a different thing. I think Java causes this confusion because it's so restrictive about what you can do with pointers. In C, the difference between a struct and a pointer to a struct is very clear. In Java, the fact that X x = new X() stores a pointer is sort of invisible.
- jwcacces 16y agoshould be chlorinate, not pollinate, we need less idiots, not more
- deleted 16y ago[deleted]
- guyzero 16y agoWhy is passing a reference by value so hard to understand?
- quicksilver03 16y agoBecause, as Joel Spolsky said once, some people are born without the part of the brain that understands pointers.