This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Swapping objects in Java

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I go about this without using an array?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Marshal
Posts: 75637
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75637
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!".
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75637
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ***?
 
Sheriff
Posts: 22645
123
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pointer to handle? Pointer to pointer to pointer? Ridiculous construction?
 
Martijn Verburg
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We always called *** triple dereferenced pointers, our markers just called it crap
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic