File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes swap without using temp variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "swap without using temp variable" Watch "swap without using temp variable" New topic

swap without using temp variable

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

how to swap the values without using temp variable

please can you tell me ?

Thanks & Regards,
[ May 28, 2008: Message edited by: David O'Meara ]
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Google says

Personally I'd just use a temp variable...
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks David ...i got the tricky
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
you can do it in a single step:
sandeep thatikonda

Joined: May 29, 2008
Posts: 1
you can swap as shown below without using temp

let a and b are two variables to swap for example let a=5 b= 3

step 1)a=a+b ------------ > a=5+3=8

step 2)b=a-b ------------ > b=8-3=5

step 3)a=a-b ------------ > a=8-5=3

finally a=3 ,b=5 hence swaped
Karthikeyan Ramaswamy
Ranch Hand

Joined: Apr 30, 2008
Posts: 42

you can swap like that also,

let a=5 b= 3

a=a*b ------------ > a=5*3=15

b=a/b ------------ > b=15/3=5

a=a/b ------------ > a=15/5=3

result a=3,b=5

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Welcome to JavaRanch, Sandy

I like Karthikeyan Ramaswamy's method! There is supposed to be a way to do it with the bitwise XOR operator ^ but I can't remember how to do it.
Karthikeyan Ramaswamy
Ranch Hand

Joined: Apr 30, 2008
Posts: 42
hi, Campbell

Using bitwise XOR operator

int a=10,b=20;

a = a^b;
b = b^a;
a = a^b;

result a=20,b=10

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Thank you.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15082

If you're writing a serious piece of software, please do not use tricks like the above to swap the values of variables. They are clever tricks, but nothing more than that. You gain nothing by using these tricks in any piece of software - it's not faster and doesn't use more memory than just using a temp variable.

Note that the above methods by adding / subtracting or multiplying / dividing don't work for all values. Integers can overflow, and dividing by zero leads to errors as well.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Yes, this stuff is all sorta kinda fun, but pointless.

Perhaps it was an interview question? If so, it doesn't say much for the interviewer. There are a small set of tricks like this, which people can easily learn for interviews, without actually knowing much about programming at all. Whereas many good honest candidates wouldn't know the answer, because they've been too busy working on real projects!

Interviewers should ask searching questions with longer answers, where it's much harder to fake a good answer. Of course, if the interviewer's own technical knowledge is lacking, and they're only looking for the "right" answer they've got on their crib-sheet, there's trouble there, too.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

"sandy th",

Will you please check your private messages regarding an important message?

Thank you,


How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: swap without using temp variable
It's not a secret anymore!