How to Swap Two Numbers In Java
Java Operators
- Java operators are symbols that are used to perform operations on variables and values. For example, the plus symbol (+) is used to add two values together.
- Java operators can be classified into several categories, such as arithmetic operators, assignment operators, comparison operators, logical operators, bitwise operators, and more.
- You must be familiar with following topics to understand the correspond example Such as: Java Data Types and Java Operators.
- To understand the how to Swap Two Numbers In Java, Read the Complete Article.
Steps to Swap two numbers In Java Program:
- Here are the Steps to swap two numbers in Java, You can follow these steps:
- Declare two variables, say a and b, and initialize them with the values to be swapped.
- Declare a temporary variable, say temp, and assign the value of a to it.
- Assign the value of b to a.
- Assign the value of temp to b.
- The values of a and b should now be swapped.
Java Operators:
Logical operators are used to perform logical operations on boolean values, such as the AND operator (&&), which returns true if both of its operands are true.
Bitwise operators are used to perform operations on the binary representation of values, such as the AND operator (&), which performs a bitwise AND operation on two values and returns the result.
Java Data Types (Primitive) :
A method in Java is a block of code that performs a specific task. Methods help to break a program into smaller, reusable parts, making it easier to write and maintain complex programs. Methods can also accept input arguments and return a value, which can be used in other parts of the program.
Let’s look at the Java Program to swap two numbers to perform certain operations.
Example 1: Java Program to Swap Two numbers.
Run
public class Main { public static void main(String[] args) { int num1 = 5; int num2 = 10; int temp; System.out.println("Before swapping:"); System.out.println("Num1 = " + num1); System.out.println("Num2 = " + num2); // swap the numbers using a temporary variable temp = num1; num1 = num2; num2 = temp; System.out.println("After swapping:"); System.out.println("Num1 = " + num1); System.out.println("Num2 = " + num2); } }
Output
Before swapping: Num1 = 5 Num2 = 10 After swapping: Num1 = 10 Num2 = 5
Explanation:
In this program, we declare two variables num1 and num2 with values 5 and 10 respectively. We also declare a third variable temp to store the value of num1 temporarily.
We then print the values of num1 and num2 before swapping, and then swap their values using the temporary variable temp. Finally, we print the values of num1 and num2 after swapping.
Example 2 :Java Program to Swap two Numbers.
public class Main { public static void main(String[] args) { int num1 = 50; int num2 = 90; System.out.println("Before swapping:"); System.out.println("Num1 = " + num1); System.out.println("Num2 = " + num2); // swap the numbers without using a temporary variable num1 = num1 + num2; num2 = num1 - num2; num1 = num1 - num2; System.out.println("After swapping:"); System.out.println("Num1 = " + num1); System.out.println("Num2 = " + num2); } }
Output
Before swapping: Num1 = 50 Num2 = 90 After swapping: Num1 = 90 Num2 = 50
Explanation:
In this program, we declare two variables num1 and num2 with values 50 and 90 respectively. .
We then print the values of num1 and num2 before swapping. To swap the values without using a temporary variable, we add num1 and num2 and store the result in num1.
Then we subtract num2 from num1 and store the result in num2.
Finally, we subtract num2 from num1 again and store the result in num1.
Example 3: Java Program to swap two numbers
Run
public class Main { public static void main (String[]args) { int num1 = 7878; int num2 = 8787; System.out.println ("Before swapping:"); System.out.println ("Num1 = " + num1); System.out.println ("Num2 = " + num2); // swap the numbers using a temporary variable num1 = num1 ^ num2; num2 = num1 ^ num2; num1 = num1 ^ num2; System.out.println ("After swapping:"); System.out.println ("Num1 = " + num1); System.out.println ("Num2 = " + num2); } }
Output
Before swapping: Num1 = 7878 Num2 = 8787 After swapping: Num1 = 8787 Num2 = 7878
Explanation:
The program uses the bitwise XOR operator (^) to swap the values of num1 and num2 without using a temporary variable.
This operator combines the bits of two values and returns a new value that contains the bits that are set in either value, but not in both.
By applying the XOR operator to num1 and num2 multiple times, we can swap their values.
Specifically, we set num1 to num1 ^ num2, then set num2 to num1 ^ num2 (which effectively sets num2 to the original value of num1), and finally set num1 to num1 ^ num2 again (which effectively sets num1 to the original value of num2).
After these operations, the values of num1 and num2 have been swapped, and we print them to verify the result.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment