wood burning stoves 2.0*
The moose likes Java in General and the fly likes Regarding swap two variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding swap two variable" Watch "Regarding swap two variable" New topic
Author

Regarding swap two variable

santhosh kumar vk
Ranch Hand

Joined: Aug 25, 2009
Posts: 144
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

Joined: Aug 16, 2005
Posts: 14114
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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

Joined: Mar 17, 2011
Posts: 7700
    
  20

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

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

Joined: May 22, 2012
Posts: 867
    
    5
> in C++ you can use the same to swap addresses

... or references to swap values.

Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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

Joined: May 22, 2012
Posts: 867
    
    5
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

Joined: Aug 11, 2007
Posts: 4646
    
    5

Ivan Jozsef Balazs wrote:


This sure looks like it violates the "no temp variables" rule to me.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7700
    
  20

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

Joined: May 22, 2012
Posts: 867
    
    5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding swap two variable