aspose file tools*
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 Soft Skills this week in the Jobs Discussion 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: 1512
    
    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: 14420
    
  23

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 8 API documentation
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: 40027
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: integer swapping without using temp variable, can you please reply me with a correct logic