4 ms·
You absolutely CAN write swap in Java: void <T> swap(T a, T b) { // use the Unsafe to copy a's memory to a temp buffer // then copy b's mem
by anon4 12y ago
You absolutely CAN write swap in Java:
void <T> swap(T a, T b) {
// use the Unsafe to copy a's memory to a temp buffer
// then copy b's memory to a
// then copy a's memory to b
// Caution: Don't pass Integers or anything, okay
}
Or, if the objects are Beans, just iterate the properties and swap them one by one. In fact, if you know you're dealing with beans, the two don't even have to be the same class, as long as their properties and types are the same.
- bad_user 12y agoNot by using that signature, no you can't.
- shiro 12y agoNo, it's not the same. X a = new X("foo"); X b = new X("bar"); X c = a; swap(a, b); // after this, a points to X("bar") and b points to // X("foo"), but c should remain pointing to X("foo"). If you swap the contents of the object, c would point to X("bar").