Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
rahul aditya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rahul aditya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Al rite, jeff sorry once again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic