It's not a secret anymore!*
The moose likes Programming Diversions and the fly likes integer swapping without using temp variable, can you please reply me with a correct logic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "integer swapping without using temp variable, can you please reply me with a correct logic " Watch "integer swapping without using temp variable, can you please reply me with a correct logic " New topic
Author

integer swapping without using temp variable, can you please reply me with a correct logic

rahul aditya
Greenhorn

Joined: Mar 14, 2012
Posts: 9
public class Swap {
public static void main(String[] args)
{
int a = 9;
int b = 7;
a=b;
b = (a+b)-b;
System.out.println("After swapping a = " + a + " b = " + b);
Swap s = new Swap();
}
}
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

Hi rahul aditya,

Welcome to CodeRanch!

Please UseCodeTags.

Secondly, your class Swap does not contain any members and methods, so why are you creating an object of it (that too, at the end of main method)?

Now, coming to your point, before you write the code, take a pencil and paper, and try to understand the flow of swapping (without temporary variable).

Hint : you are doing a=b in your code. At that very instance, value of a is gone. All you have is 2 variables containing value of b, and further code is useless from swapping perspective.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
rahul aditya
Greenhorn

Joined: Mar 14, 2012
Posts: 9
Thank you sir, it really did help me
i just found out the logic to be
a=a+b;
b=a-b;
a=a-b;
thank you very much
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Another way is XOR swap.

Note that while these are fun tricks, you should never use this in any real program, because code like this is unnecessarily complicated and confusing and you are not gaining anything in efficiency or performance.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

rahul aditya wrote:public class Swap {
public static void main(String[] args)
{
int a = 9;
int b = 7;
a=b;
b = (a+b)-b;
System.out.println("After swapping a = " + a + " b = " + b);
Swap s = new Swap();
}
}


Well, looks like interesting question for beginners like me.
hmm....,So, if you want a single line logic so i think the correct logic should be like this.....
MODERATOR ACTION: code removed


Nikhil Sagar, I've already asked you once: DontBeACodeMill(⇐click) and LetThemDoTheirOwnHomework(⇐click). You apologized and said you wouldn't do it again. Please honor that pledge. Thank you.

OCPJP 6 86%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38039
    
  22
You do realise this is not something you ought to do in real-life code? I shall move this discussion to where we usually discuss such questions.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Al rite, jeff sorry once again.
 
wood burning stoves
 
subject: integer swapping without using temp variable, can you please reply me with a correct logic
 
Similar Threads
Swap function in java
small swapping problem please please reply!!!!!
object r passed by reference ???-check this and u would doubt it
how to swap two integers inside a function ?
Fill the code