aspose file tools*
The moose likes Java in General and the fly likes Swapping objects in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Swapping objects in Java" Watch "Swapping objects in Java" New topic
Author

Swapping objects in Java

Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
How would I go about this without using an array?


Technology can never substitute for knowledge.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40



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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

If you read This JavaRanch article you may get an understanding of why what Henry said is true.


Steve
Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Mar 27, 2003
Posts: 836
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


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Jun 24, 2003
Posts: 3274
    
    5

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

Joined: Mar 22, 2000
Posts: 12806
    
    5
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Oct 27, 2005
Posts: 19722
    
  20

Pointer to handle? Pointer to pointer to pointer? Ridiculous construction?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

We always called *** triple dereferenced pointers, our markers just called it crap
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swapping objects in Java