4 ms·
Yes it is the exact same swap. Either behavior (what you call a3 == a2 and a3 == a1) is possible in either C++ or Java, depending on what you mean by "assign a
by solipsism 12y ago
Yes it is the exact same swap. Either behavior (what you call a3 == a2 and a3 == a1) is possible in either C++ or Java, depending on what you mean by "assign a1 to a new variable a3". Your confusion is surely that C++'s assign-by-value looks like Java's assign-by-reference. Again, either behavior is achievable
May I invite you to write some code and see for yourself? Here:
C++ Swap (what you and the article call the litmus test):
https://gist.github.com/anonymous/06b9251a37008d158f59 https://gist.github.com/anonymous/06b9251a37008d158f59
Java Swap (exactly the same thing):
https://gist.github.com/anonymous/d57d272579079b4e9ee8 https://gist.github.com/anonymous/d57d272579079b4e9ee8
These programs don't only output the exact same thing, they do exactly the same thing (except the differences I noted above, about the default copy assignment operator being implicit in C++, and about C++ putting the temp variable on the stack instead of the heap).
- jjnoakes 12y agoNo, it isn't the same swap. If you actually understand what is going on, you'd realize it. > May I invite you to write some code and see for yourself? Sure. I've fixed your examples so they are analogous to each other and added the check that I described in my previous post. C++: https://gist.github.com/anonymous/9c66999844afcab15eeb https://gist.github.com/anonymous/9c66999844afcab15eeb Java: https://gist.github.com/anonymous/bac324b3f3c13f263e14 https://gist.github.com/anonymous/bac324b3f3c13f263e14
- solipsism 12y agoEdited because I responded before reading your "fixes" correctly.
- solipsism 12y agoWait. 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/ http://www.cplusplus.com/reference/algorithm/swap/
- jjnoakes 12y agoYeah, 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 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".
- solipsism 12y agoAre 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!
- jjnoakes 12y agoYou 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'.