|
![]() |
Technology can never substitute for knowledge.
Technology can never substitute for knowledge.
Steve
Technology can never substitute for knowledge.
Originally posted by Christopher Young:
swap(Obj a, Obj b) {
Obj c = a;
b = a;
c = b;
}
I'd like to add that C++ always passes by value. It has a "pass by reference" concept, but it's a compiler trick and not a fundamental concept (like pointers are). The difference with C/C++ is that you can use (and pass the value of) pointers to data types/objects, and even pointers-to-pointers. Doing the latter allows you to swap two object references over. Like this:Java can't have "pointers-to-pointers", hence why this doesn't work. Sometimes, but not often, that's a disadvantage too, because Java can't have "out parameters". But you could simulate it like this (it is pointless by the way!):But then all your variables are of type Reference<T>, which is just confusing :roll: You can obviously do the same with an Object[] as the argument, because you can re-reference the elements in the array to do the swap, just like I re-referenced the 'obj' member in Reference<T>.He said something about trying to swap objects in Java without using an array.
Originally posted by Charles Lyons:
I'd like to add that C++ always passes by value. It has a "pass by reference" concept, but it's a compiler trick and not a fundamental concept (like pointers are). The difference with C/C++ is that you can use (and pass the value of) pointers to data types/objects, and even pointers-to-pointers. Doing the latter allows you to swap two object references over. Like this:Java can't have "pointers-to-pointers", hence why this doesn't work. Sometimes, but not often, that's a disadvantage too, because Java can't have "out parameters". But you could simulate it like this (it is pointless by the way!):But then all your variables are of type Reference<T>, which is just confusing :roll: You can obviously do the same with an Object[] as the argument, because you can re-reference the elements in the array to do the swap, just like I re-referenced the 'obj' member in Reference<T>.
References in C++ (denoted by myclass& for example) are essentially equivalent to "pass by reference" in Java because a reference is really a single pointer (like void*) and not a pointer-to-pointer. So using a reference in C++ also wouldn't allow you to swap two variables over... you'd still need a pointer to the reference![]()
Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Originally posted by Martijn Verburg:
Ah that brings back memory of the *** declarations in my Undergraduate code, I've learned better now![]()
Originally posted by Campbell Ritchie:
I thought *** was what you put in the middle of a naughty word. I now know where it comes from![]()
Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Swapping objects in Java
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Honk if you love justice! And honk twice for tiny ads!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
|