This is a Java program that illustrates situations where two numbers will be swapped.

Explaining Different Swapping Methods: 

Using a Temporary Variable
  • Store a number in a temporary variable.
  • Swap the values by assigning them correctly as follows exactly the same workmethod 
Without a Temporary Variable (Using Arithmetic):
  • Adding and subtracting to swap in arithmetic
  • Risk: Possible overflow for large number if that number you see is right due to introduction extra space (for temporary variable)
Using Bitwise XOR:
  • XOR is an efficient solution to swapping numbers.
  • Applies to int types
Using a Single Statement:
  • use of arithmetic.
  • Single line swap, no extra variable required.

Swap two numbers in Java example program 

  1. import java.util.Scanner;

  2. public class SwapNumbers {
  3.     
  4.     // Swap two numbers in java using a temporary variable
  5.     public static void swapWithTemp(int a, int b) {
  6.         System.out.println("Before Swap: a = " + a + ", b = " + b);
  7.         int temp = a;
  8.         a = b;
  9.         b = temp;
  10.         System.out.println("After Swap: a = " + a + ", b = " + b);
  11.     }

  12.     // Swap two numbers without using a temporary variable (using arithmetic operations)
  13.     public static void swapWithoutTemp(int a, int b) {
  14.         System.out.println("Before Swap: a = " + a + ", b = " + b);
  15.         a = a + b;
  16.         b = a - b;
  17.         a = a - b;
  18.         System.out.println("After Swap: a = " + a + ", b = " + b);
  19.     }
  20.     
  21.     // Swap two numbers using bitwise XOR operator
  22.     public static void swapWithXOR(int a, int b) {
  23.         System.out.println("Before Swap: a = " + a + ", b = " + b);
  24.         a = a ^ b;
  25.         b = a ^ b;
  26.         a = a ^ b;
  27.         System.out.println("After Swap: a = " + a + ", b = " + b);
  28.     }

  29.     // Swap two numbers in java using a single statement (tuple swap)
  30.     public static void swapSingleStatement(int a, int b) {
  31.         System.out.println("Before Swap: a = " + a + ", b = " + b);
  32.         b = (a + b) - (a = b);
  33.         System.out.println("After Swap: a = " + a + ", b = " + b);
  34.     }

  35.     public static void main(String[] args) {
  36.         Scanner scanner = new Scanner(System.in);
  37.         System.out.print("Enter first number: ");
  38.         int num1 = scanner.nextInt();
  39.         System.out.print("Enter second number: ");
  40.         int num2 = scanner.nextInt();

  41.         System.out.println("\nSwapping using a temporary variable:");
  42.         swapWithTemp(num1, num2);

  43.         System.out.println("\nSwapping without using a temporary variable:");
  44.         swapWithoutTemp(num1, num2);

  45.         System.out.println("\nSwapping using XOR operator:");
  46.         swapWithXOR(num1, num2);
  47.         
  48.         System.out.println("\nSwapping using a single statement:");
  49.         swapSingleStatement(num1, num2);

  50.         scanner.close();
  51.     }
  52. }


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu