Introduction
Swapping two numbers is a basic programming task that can be accomplished in several ways. This guide will show you how to swap two numbers in Java using different methods, including using a temporary variable, arithmetic operations, and bitwise XOR.
Problem Statement
Given two numbers, swap their values so that the first number takes the value of the second, and the second takes the value of the first.
Example:
- Input:
a = 5
,b = 10
- Output:
a = 10
,b = 5
Solution Steps
Approach 1: Using a Temporary Variable
- Store the First Number: Use a temporary variable to store the value of the first number.
- Swap the Values: Assign the value of the second number to the first number, and then assign the value stored in the temporary variable to the second number.
Approach 2: Using Arithmetic Operations
- Add the Numbers: Add both numbers and store the result in the first variable.
- Subtract the Second Number: Subtract the second number from the first variable and store the result in the second variable.
- Subtract the New Second Number: Subtract the new second number from the first variable and store the result in the first variable.
Approach 3: Using Bitwise XOR
- XOR the Numbers: XOR both numbers and store the result in the first variable.
- XOR the First Variable with the Second: XOR the first variable with the second and store the result in the second variable.
- XOR the First Variable with the Second Again: XOR the first variable with the second and store the result in the first variable.
Approach 1: Using a Temporary Variable
/**
* Java Program to Swap Two Numbers using a Temporary Variable
* Author: https://www.javaguides.net/
*/
public class SwapUsingTempVariable {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swap: a = " + a + ", b = " + b);
// Using a temporary variable to swap
int temp = a;
a = b;
b = temp;
System.out.println("After swap: a = " + a + ", b = " + b);
}
}
Output for Approach 1
Before swap: a = 5, b = 10
After swap: a = 10, b = 5
Explanation for Approach 1
- Store the First Number: The value of
a
is stored in the temporary variabletemp
. - Swap the Values: The value of
b
is assigned toa
, and the value intemp
(original value ofa
) is assigned tob
.
Approach 2: Using Arithmetic Operations
/**
* Java Program to Swap Two Numbers using Arithmetic Operations
* Author: https://www.javaguides.net/
*/
public class SwapUsingArithmetic {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swap: a = " + a + ", b = " + b);
// Swapping using arithmetic operations
a = a + b; // a now becomes 15
b = a - b; // b now becomes 5 (15 - 10)
a = a - b; // a now becomes 10 (15 - 5)
System.out.println("After swap: a = " + a + ", b = " + b);
}
}
Output for Approach 2
Before swap: a = 5, b = 10
After swap: a = 10, b = 5
Explanation for Approach 2
- Add the Numbers: The sum of
a
andb
is stored ina
. - Subtract the Second Number: The value of
b
is subtracted froma
(which now contains the sum), and the result is stored inb
. - Subtract the New Second Number: The value of
b
(which now holds the original value ofa
) is subtracted froma
(which still contains the sum), and the result is stored ina
.
Approach 3: Using Bitwise XOR
/**
* Java Program to Swap Two Numbers using Bitwise XOR
* Author: https://www.javaguides.net/
*/
public class SwapUsingXOR {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swap: a = " + a + ", b = " + b);
// Swapping using bitwise XOR
a = a ^ b; // a now becomes 15 (binary: 1111)
b = a ^ b; // b now becomes 5 (binary: 0101)
a = a ^ b; // a now becomes 10 (binary: 1010)
System.out.println("After swap: a = " + a + ", b = " + b);
}
}
Output for Approach 3
Before swap: a = 5, b = 10
After swap: a = 10, b = 5
Explanation for Approach 3
- XOR the Numbers: The XOR of
a
andb
is stored ina
. - XOR the First Variable with the Second: The XOR of the new
a
(which now contains the XOR of the originala
andb
) andb
is stored inb
, givingb
the original value ofa
. - XOR the First Variable with the Second Again: The XOR of the new
a
(which now contains the XOR of the originala
andb
) and the newb
(which now contains the original value ofa
) is stored ina
, givinga
the original value ofb
.
Conclusion
These three approaches demonstrate different ways to swap two numbers in Java. The first approach using a temporary variable is the most straightforward and easiest to understand. The second approach, using arithmetic operations, avoids using extra space but may lead to overflow with large numbers. The third approach using bitwise XOR is a clever technique that also avoids extra space but may be less intuitive for beginners. Each method is effective, and the choice of which to use depends on the specific requirements and constraints of your program.
Related Java Programs
String Programs in Java with OutputJava Program to Count Number of Duplicate Words in String
Java Program to Count Number of Words in Given String
Java Program to Count the Number of Occurrences of Substring in a String
Java Program to Count the Occurrences of Each Character in String
Java Program to Merge two String Arrays
Java Program to Remove Duplicate Words from String
Java Program to Reverse a String(5 ways)
Java Program to Reverse Each Word of a String
Java Program to Swap Two Strings
How to Check if the String Contains only Digits
How to Check if the String Contains only Letters
How to Check If the String Contains Only Letters or Digits
Java Program to Check if Input String is Palindrome
Java Program to Find all Permutations of String
How to Remove or Trim All White Spaces from a String in Java
How to Remove Leading and Trailing White Space From a String in Java
Java Program to Count Duplicate Characters in a String
Remove Character from String in Java (Java 8)
Java Program to Count Vowels and Consonants in a String (Java 8)
4 Ways to Find First Non-Repeated Character in String in Java
Comments
Post a Comment
Leave Comment