6 ms·
When I was interviewing java devs of all levels few (less than 10%, I'd say) could flat out say "it's pass by value for everything, for non-primitives you're pa
by dschobel 17y ago
When I was interviewing java devs of all levels few (less than 10%, I'd say) could flat out say "it's pass by value for everything, for non-primitives you're passing references by value" but a lot more (roughly half) could trace the following code correctly.
Object x = null;
makeString (x);
System.out.println (x);
void makeString (Object y)
{
y = "This is a string";
}
Maybe it's just a disconnect between theory and practice?
- moss 17y agoThis. Java sets up an environment where the parameter passing behavior just isn't surprising. If you've been programming in Java for a little while, it's very easy to have an intuitive feel for how parameters will act, without having to go back to theoretical principles. Knowing about the theory behind pointers, pass-by-value, and pass-by-reference will still make you a better programmer. But Java's a stronger language for not requiring that knowledge.
- kragen 17y agoYou're replying to a comment that says "interviewing java devs of all levels... a lot more (roughly half) could trace the following code correctly." That means that the other (roughly) half of the "Java devs" interviewed found Java's pass-by-value behavior to be surprising, even though there are no exceptions to it. So although Java's behavior is simple and consistent, the parent comment seems to flatly contradict your assertion, "If you've been programming in Java for a little while, it's very easy to have an intuitive feel for how parameters will act." It's also a perfect example of how programming in Java, or in fact any language with a parameter-passing mechanism and mutable variables, does require that knowledge, so your assertion that "Java's a stronger language for not requiring that knowledge" is nonsense.
- moss 17y agoYou're right: calling it "very easy" was overstating things to the point of absurdity. I do think Java's behavior has a big advantage in being easier to understand than the theory behind it is ("less than 10%"), but I had completely glossed over the "roughly half" point the first time I read this, and it really hurts my claim that Java's approach is trivial to understand. That said, I stand by "not requiring that knowledge". Java's behavior is understandable without having to know about value and reference semantics. That doesn't mean it doesn't require any knowledge. Specifically, I think it requires two points: (1) Variables are just names, so reassigning a variable doesn't change anything else in the system; (2) When you call a method on an object, you might change that object. In support of this: I've also been doing a lot of interviews recently. In my (admittedly limited) experience, the people who have had trouble with Java's parameter passing behavior are more experienced developers who've done a lot of work in other languages. Their confusion isn't from Java's behavior, it's the caution about pass-by-reference that they've brought over from C and C++. I don't really see junior people making this mistake. So I suspect having just a little knowledge of the value vs. reference issue makes Java harder to understand, by bringing up new and confusing possibilities. But, like I said, my experience is limited. If you do see new developers being confused by this aspect of Java (or Python, Ruby, or other languages with similar semantics), I'd be interested to hear about it.
- raganwald 17y agoThis!? Was I asleep when "this" meme swept through the internet? Help me out: what do you mean when you say "This."???
- iron_ball 17y ago"The comment to which I am replying is correct."
- raganwald 17y agoOh dear, I was using the little up arrow for that :-)
- moss 17y agoWhat iron_ball said, plus a share of "I agree so strongly that I almost want to just repeat everything you said". Alternately, it could mean: "I was reading LiveJournal five minutes ago, and haven't quite finished switching over to the Hacker News house style." ;)
- deleted 17y ago[deleted]
- dkarl 17y agoYes. Practical programming books usually don't talk about "pass by reference" or "pass by value" anymore, because (as people have pointed out) it's rarely a useful distinction these days, and the correct usage just confuses people because people take pass by value for granted. (C++ programmers basically see references as restricted, syntactically sweetened pointers that are passed by value. And that's almost always good enough.) I use this as an interview question, too ("Is Java a pass-by-reference or pass-by-value language?") Nobody ever gets it, but I let people choose one of several languages to be quizzed about, and the ones who know this kind of terminology are usually game to pick a more difficult language. I don't hold it against them as long as their explanation of why Java is pass by reference shows a correct understanding of Java parameter passing. But I always set them straight afterwards ;-)
- rbanffy 17y agoI think that, instead of hiring those who correctly figure out what happens in this code, you should hire also those who have strong, almost allergic, reactions against it. Do you really maintain stuff like this in your codebase?