Java program to swap two numbers is a very basic example and can be used in any programming language.
This is a beginner-level question for all programmers.
Here different ways are given for swapping two numbers.
All below java programs shows only method to implement this.
Swapping of two numbers can be done in any language.
1 Swapping two numbers using a third variable
1 2 3 4 5 6 7 8 9 10 11 | public class SwapExample { public static void main(String[] args) { int a = 5, b = 7; int c; System.out.println("a= " + a + " b=" + b); c = a; a = b; b = c; System.out.println("a= " + a + " b=" + b); } } |
This is a simple and most commonly used method where three variables are taken.
The first variable value is stored in the third variable now the second variable value is stored in variable first and again temporary variable (here c ) value is stored in the second variable.
Output
1 2 | a= 5 b=7 a= 7 b=5 |
2. Swapping two numbers without using third variable
1 2 3 4 5 6 7 8 9 10 11 | public class SwapExample1 { public static void main(String[] args) { int a = 5, b = 7; int c; System.out.println("a= " + a + " b=" + b); a = a + b; b = a-b; a=a-b; System.out.println("a= " + a + " b=" + b); } } |
Here no extra variables are taken. two variables are added and stored in the first variable.
Now the second variable is subtracted from the first and stored in the second variable.
Similarly way second is again subtracted from the first and stored in the first variable.
Output
1 2 | a= 5 b=7 a= 7 b=5 |
3. Swapping two numbers in a single line using addition and subtraction
1 2 3 4 5 6 7 8 9 | public class SwapExample1 { public static void main(String[] args) { int a = 5, b = 7; int c; System.out.println("a= " + a + " b=" + b); b = (a + b) - (a = b); System.out.println("a= " + a + " b=" + b); } } |
Here all operation is performed in a single line as given b = (a + b) - (a = b)
Output
1 2 | a= 5 b=7 a= 7 b=5 |
4. Swapping two numbers in a single line using multiplication and division
1 2 3 4 5 6 7 8 9 10 | public class SwapExample1 { public static void main(String[] args) { int a = 5, b = 7; int c; System.out.println("a= " + a + " b=" + b); a = (a * b) / (b = a); System.out.println("a= " + a + " b=" + b); } } |
Here multiplication and division operation is used a=(a * b)/(b=a)
.
Output
1 2 | a= 5 b=7 a= 7 b=5 |
5. Swapping two numbers using multiplication and division
Here numbers must be non zero
1 2 3 4 5 6 7 8 9 10 11 | public class SwapExample1 { public static void main(String[] args) { int a = 5, b = 7; int c; System.out.println("a= " + a + " b=" + b); a = a * b; b = a / b; a = a / b; System.out.println("a= " + a + " b=" + b); } } |
swapping of two numbers can also be achieved using multiplication and division operations as given above.
Output
1 2 | a= 5 b=7 a= 7 b=5 |
6. Swapping two numbers using XOR operator
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class SwapExample1 { public static void main(String[] args) { int a = 5, b = 7; int c; System.out.println("a= " + a + " b=" + b); a = a ^ b; b = a ^ b; a = a ^ b; System.out.println("a= " + a + " b=" + b); } } |
above operation can also be written as
Output
1 2 3 | a ^= b; b ^= a; a ^= b; |
Here XOR short hand operator is used.
Output
1 2 | a= 5 b=7 a= 7 b=5 |
Here Java program to swap two numbers are discussed.