Single line expressions to swap two integers in Java

Given two integers, swap them in single line in Java.

 

 
There are many expressions to swap two variables in single line in Java. Notice that all below methods can also be used with C/C++.


 

1. Using Bitwise XOR(^) Operator

 

Download   Run Code

 

2. Using Addition and Subtraction Operator

 

Download   Run Code

 

3. Using Multiplication and Division Operator

 

Download   Run Code

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 


Leave a Reply

avatar
  Subscribe  
Notify of