Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding swap two variable

 
santhosh kumar vk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could you please tell me how to swap two variable in java without using temp variable using any formula.

thanks,

Santhosh Kumar V.K
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this one of those silly interview questions that you got somewhere?

There's a trick to swap for example integer values without using a temporary value, using XOR.

It's a trick, and it has no practical value whatsoever, and it doesn't tell the interviewer anything about the candidate - only that he or she knows the trick. In other words, it's stupid to ask a question like that for a job interview.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a Swapper class, but I think that would count as “temporary variables.” If, as Jesper says, you are asked that sort of question in an interview, and the job hangs on that question, maybe you should decline the offer.
 
Winston Gutkowski
Bartender
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santhosh kumar vk wrote:Could you please tell me how to swap two variable in java without using temp variable using any formula.

You can't.

As Jesper says, you can swap two primitives using the XOR trick, and in C++ you can use the same to swap addresses; but Java doesn't have addresses, so if the variable is a reference type, it can't be done.

As others have said: Bad interview question - very bad.

Winston
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I first saw the XOR hack in 1973 or so. But I don't know if it ever had any real world justification. In those days, we were usually programming mainframes, and memory was insanely expensive, but even then, you could usually find a register or make a temp location on the stack.

Most modern processors have a swap instruction, usually used to implement a semaphore. So there is even less reason to use the XOR hack.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> in C++ you can use the same to swap addresses

... or references to swap values.

 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:> in C++ you can use the same to swap addresses

... or references to swap values.

. . .
Of course, that is not possible in Java because Java does not support pass‑by‑reference.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A question: can the following statements be arranged in an appropriate order to achieve swapping?




Applying the i += j; and i -= j; operations on the (i,j) vector is applying the operations with the matrices
|1 1|
|0 1|

|1 -1|
|0 1|

j += i; and i -= i; correspond to
|1 0|
|1 1|

|1 0|
|-1 1|

So the question is also can these matrices be arranged so that their product is the swapping matrix:
|0 1|
|1 0|

And the answer is no: the determinants get multiplied and ones do not yield -1.



 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:


This sure looks like it violates the "no temp variables" rule to me.
 
Winston Gutkowski
Bartender
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:I first saw the XOR hack in 1973 or so. But I don't know if it ever had any real world justification.

Well, one place where I think it still does is for linked lists, because you can store a doubly-linked node with only one pointer; although, according to this page, alternatives such as unrolled linked lists are probably just as good.

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:
This sure looks like it violates the "no temp variables" rule to me.


My contribution was an example for C++ references and obviously not for the no-temp exercise.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic