Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swapping objects in Java

 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I go about this without using an array?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you elaborate a bit? Are you trying to answer an interview question like "How would you write a Java method to swap two variables?" or something along those lines?
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been in debate with someone over the technical differences in C++ and Java when it comes to parameter passing. He said something about trying to swap objects in Java without using an array.

I tried something like

swap(Obj a, Obj b) {
Obj c = a;
b = a;
c = b;
}

but no luck, though. When I read out the values for the objects in the caller method they were the same.
[ December 13, 2008: Message edited by: Christopher Young ]
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Unfortunately, if you are arguing that you can exchange the references, without using an array, and anything that "indirects" the references, you lost the debate. Your friend is right.

Henry
[ December 13, 2008: Message edited by: Henry Wong ]
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read This JavaRanch article you may get an understanding of why what Henry said is true.
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Christopher Young:
swap(Obj a, Obj b) {
Obj c = a;
b = a;
c = b;
}


Please use the code tags to maintain indentation.

What you would use in C is like thisI have used stars to mark the difference; the stars themselves are the difference. Only that still won't work. I'll leave you to work out why!
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He said something about trying to swap objects in Java without using an array.
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
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Ah that brings back memory of the *** declarations in my Undergraduate code, I've learned better now
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Martijn Verburg:


Ah that brings back memory of the *** declarations in my Undergraduate code, I've learned better now


I thought *** was what you put in the middle of a naughty word. I now know where it comes from
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Well it would provoke that reaction from the Graduate marker, e.g. "Why did you *** use ***void in your code!".
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my less than humble opinion, the whole gefuffle is due to an incorrect statement of the problem:
Swapping objects in Java


A correct statement of the problem as:
Swapping references to objects in Java
automatically leads you in the right direction.

Bill
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been shown a C program with function pointers in which is supposed to work exactly the same if you write f() *f() **f() or ******************f().

I know * is a pointer and ** is a handle. But is there a name for ***?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pointer to handle? Pointer to pointer to pointer? Ridiculous construction?
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We always called *** triple dereferenced pointers, our markers just called it crap
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic