4 ms·
You can absolutely make a swap for reference types in Java. Here: https://gist.github.com/anonymous/d3b4b3bcacb478896ef2 https://gist.github.com/anonymous/d3b
by solipsism 12y ago
You can absolutely make a swap for reference types in Java. Here: https://gist.github.com/anonymous/d3b4b3bcacb478896ef2 https://gist.github.com/anonymous/d3b4b3bcacb478896ef2
This is analogous to a C++ swap(Type&,Type&). The main difference is that C++ provides a default assignment operator for types, meaning you don't have to provide Assign yourself. A smaller difference is that the temporary instance can be created on the stack in C++ but not in Java.
For primitive types it's absolutely true that you cannot implement swap (unless you were to have created that value type in a reference-type wrapper). It's misleading to say you can't do so for reference types.
- jjnoakes 12y agoThis is not the same swap. The article (and the correct pass-by-reference litmus test) swaps the values of the variables as the caller sees them, using operations inside the swap routine. Your example swapped the contents of the pointed-to objects, but did not change the values of the variables a1 and a2. If you want to see the difference, assign a1 to a new variable a3 before the a1/a2 swap call. A real pass by reference swap will end up with a3 == a2, and your example ends up with a3 == a1.
- solipsism 12y agoYes 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".