Introduction to Swapping in Java
As we all know, Java is one of the most trending languages for software development. Understanding its some frequently used concepts is very important and can help the programmer use them even in solving large problems. Swapping is one of the concepts which is frequently used in programs. Swapping means the interchanging of values of variables. It is done with the data in memory. Swapping can be done either with the help of one temporary variable or without using the temporary variable. Most of the sorting and searching algorithms use swapping the values of variables. In this topic, we are going to learn about Swapping in Java.
Swapping of Two Numbers in Java
Let us take a look with the help of some example.
Case 1: Swapping of numbers using the temporary variable
Code:
public class Swap2Numbers
{
public static void main(String[] args)
{
int num1=10;
int num2 =20;
// Printing values of numbers before swapping in order to see the change
System.out.println("Original values before swapping are:");
System.out.println("Value of number 1 is " +num1); System.out.println("Value of number 2 is " +num2);
// Value of num1, i.e. 10 is assigned to temp variable int temp = num1;
// Value of num2, i.e. 20 is assigned to num1 variable num1 = num2;
// Value of temp variable, i.e. 10 (assigned by num1) is assigned to num2
int temp = num1; num1 = num2;
num2 = temp;
// Printing values of numbers after swapping in order to see the change
System.out.println("Values after swapping are:");
System.out.println("Value of number 1 is " +num1);
System.out.println("Value of number 2 is " +num2);
}
}
Output:
Explanation:
In the above program, we had two variables named num1 and num2 with values of 10 and 20, respectively. A temporary variable with the name temp is used of the same data type of the variables num1 and num2. The swapping is processed in 3 steps:
- The value of ‘num1’ (i.e. 10) is assigned to the temporary variable ‘temp’, so now the value of ‘temp’ is 10.
- The value of ‘num2’ (i.e. 20) is assigned to the ‘num1’ variable, i.e. now the value of the ‘num1’ variable is 20.
- The value of the ‘temp’ variable (i.e. 10) assigned in step 1 is now assigned to the ‘num 2’ variable, i.e. the value of the ‘num2’ variable becomes 10 now.
Finally, the values of the variables are swapped or interchanged, and the swapped values are printed on the console.
Case 2: Swapping of two numbers without using the temporary variable
Code:
public class SwapNumbers
{
public static void main(String[] args)
{
int num1=10;
int num2 =20;
// Printing values of numbers before swapping in order to see the change
System.out.println("Original values before swapping are:");
System.out.println("Value of number 1 is " +num1); System.out.println("Value of number 2 is " +num2);
num1 = num1- num2; num2 = num1+ num2; num1 = num2- num1;
// Printing values of numbers after swapping in order to see the change
System.out.println("Values after swapping are:");
System.out.println("Value of number 1 is " +num1);
System.out.println("Value of number 2 is " +num2);
}
}
Output:
Explanation:
In the above example, simple mathematics is followed to swap the numbers, which is done in 3 steps:
- Value of num1- num2 (i.e. 10- 20 = -10) is stored in the ‘num1’ variable. Now num1= -10.
- Value of num1+ num2 (i.e. -10+20 = 10) stored in the ‘num2’ variable. Now num2= 10.
- Value of num2- num1 (i.e. 10 – (-10))= 20) is stored in the ‘num1’ variable. Now, num1=20.
Swapping of Three Numbers in Java
Let us study swapping of three number with the help of some example
Case 1: Swapping of 3 numbers without using a temporary variable
Code:
public class Swap3Numbers
{
public static void main(String[] args)
{
int num1= 10; int num2= 20; int num3= 30;
// Printing values of numbers before swapping in order to see the change
System.out.println("Original values before swapping are:");
System.out.println("Value of number 1 is " +num1);
System.out.println("Value of number 2 is " +num2); System.out.println("Value of number 3 is " +num3);
num1 = num1+ num2+ num3; num2 = num1- (num2+ num3); num3 = num1- (num2+ num3); num1 = num1- (num2+ num3);
// Printing values of numbers after swapping in order to see the change
System.out.println("Values after swapping are:");
System.out.println("Value of number 1 is " +num1);
System.out.println("Value of number 2 is " +num2);
System.out.println("Value of number 3 is " +num3);
}
}
Output:
Explanation:
In the above example, simple mathematics is followed to exchange the value of 3 variables. It involves 4 steps which are mentioned below:
- Value of num1+ num2+ num3 (i.e. 10 + 20 + 30 = 60) is assigned to variable ‘num1’. So num1 = 60.
- Value of num1- (num2+ num3) (i.e. 60 – (20+30) = 10) is assigned to variable ‘num2’. So num2= 10.
- Value of num1- (num2+ num3) (i.e. 60 – (10+30) = 20) is assigned to variable ‘num3’. So num3= 20.
- Value of num1- (num2+ num3) (i.e. 60 – (10+20) = 30) is assigned to variable ‘num1’. So num1= 30.
The swapped values of the 3 variables are printed on the console.
Case 2: Swapping of 3 numbers using a temporary variable
Code:
public class Swap3Numbers
{
public static void main( String[] args)
{
int num1=10; int num2 =20; int num3 =30;
// Printing values of numbers before swapping in order to see the change
System.out.println("Original values before swapping are:");
System.out.println("Value of number 1 is " +num1);
System.out.println("Value of number 2 is " +num2); System.out.println("Value of number 3 is " +num3);
int temp = num1; num1 = num2; num2= num3;
num3= temp;
// Printing values of numbers after swapping in order to see the change
System.out.println("Values after swapping are:");
System.out.println("Value of number 1 is " +num1);
System.out.println("Value of number 2 is " +num2); System.out.println("Value of number 3 is " +num3);
}
}
Output:
Explanation:
In the above program, swapping of 3 numbers is performed in 4 simple steps, and a temporary variable ‘temp’ is used:
- The value of num1 (i.e. 10) is assigned to the temp variable. So, now the temp has a value of 10.
- Value of num2 variable (i.e. 20) is assigned to num1, so num1 value is 20 now.
- Value if num3 variable (i.e. 30 ) is assigned to num2 variable, so num2 has value 30.
- The value of the temp variable (i.e. 10) is assigned to the num3 variable, so num3 has value 10 now.
Values of the 3 numbers are swapped and printed on the console.
Conclusion
As explained above, swapping is performed in the above two ways mentioned. However, the swapping in Java can also be done by using call-by-reference, which uses the address of numbers to value the values.
Recommended Articles
This is a guide to Swapping in Java. Here we discuss the Swapping of Two Numbers and Three Numbers in Java with and without using temporary variables. You may also have a look at the following articles to learn more –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses