Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Wait. This is even crazier than I thought. I just looked closer at your "fixes". You're passing a pointer by reference? You're really stretching.

Look at the article. It contains c++ code for a swap litmus test. It doesn't look like your "fix". Your swap is not the traditional swap(a,b) method/function called for by the author.

Here is a traditional swap function: http://www.cplusplus.com/reference/algorithm/swap/



Yeah, no kidding. You can't write the C++ by-value swap (without pointers) in Java because Java is only pointers. It has no non-pointer objects.

The C++ version I posted shows you exactly what the Java object model is doing, except since you can't take in references to the variables (Java has no pass-by-reference), you can't do the pass-by-reference swap. The only swap you can do in Java is the pass-by-value swap.

The C++ pass-by-value swap (using pointers like Java requires) looks like this:

https://gist.github.com/anonymous/73ccac277baee8841470

Note that this now prints "Fake pass-by-value swap" like the Java version.

There is no way to write a Java version that prints "Real pass-by-reference swap".


Are you making the argument that C++ has pass-by-reference and Java does not? No one disputes this. That's not my point. My point is whether the ability to write a traditional "swap" is a good litmus test.

I've proved that you can write "swap" in Java. This behavior is in all important ways equivalent to the basic, traditional C++ swap, even though Java passes references by value and C++ passes by reference. You're arguing, "Yeah but look, they look different! If you put the exact same comparison code afterward, they return different things!" Well... yeah... that's because Java and C++ are different! Apples and oranges. That has nothing to do with whether the ability to write "swap" is a good litmus test.

The article says you cannot write "swap" in Java, therefore Java doesn't have pass-by-reference. I'm saying "yes, you can write 'swap' in Java.. but still Java doesn't have pass-by-reference.. so it's a bad litmus test." And you're saying "Yeah but your swap looks different." And I'm saying "No shit, that's because Java doesn't have pass-by-reference." The article didn't say the litmus test is "can you write swap in a way that looks exactly like this C++ code?".

That's it for me, I'm bowing out!


You can't write swap in Java as defined by the article - one that changes the values of the parameters. Your swap only follows the pointers and changes the pointed-to objects.

A valid, real "swap" in Java is one that passes my example code and prints "real pass-by-reference swap" and not "fake pass-by-value swap'.


Oh, and it's not stretching at all. Take the swap in your article and instantiate it with

    T=A*
and you'll see that the arguments to the swap become

    A*&




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: