Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Program to demonstrate how to swap two numbers with and without using third variable

Introduction:-
Hello friends, In this section of tutorial we will go over and look into two quite simple programs:
  • How to Swap two numbers in Java ?.
  • How to Swap two numbers in Java without using third variable ?. 
In the first program we will swap two numbers with the use of third variable called as temp. It is generally a temporary local variable which can store value of a number for sometime and assign it to a different variable.
The code to swap two numbers by using third variable is written in swap method in below program. In the swap method our both the numbers i.e number1 and number2 are passed as arguments. In the swap method a temp variable is created to which number1 value is assigned to store it for sometime, so that it can assign it to number2 which will help us in performing swapping of two numbers.Meanwhile, number2 value is directly assigned to number1.




Program to demonstrate how to swap two numbers in Java
package com.hubberspot.swap.example;

/**
 *
 * @author jontymagicman
 */
public class Swap {
  public static void main(String[] args) {
    int number1 = 5;
    int number2 = 9;
    
    System.out.println("Before Swapping the numbers : ");
    System.out.println("Value of number1 is :" + number1);
    System.out.println("Value of number2 is :" +number2);
    
    swap(number1, number2);
  }
  
  private static void swap(int number1, int number2) {
    int temp = number1;
    number1 = number2;
    number2 = temp;
    
    System.out.println("After Swapping the numbers : ");
    System.out.println("Value of number1 is :" + number1);
    System.out.println("Value of number2 is :" +number2);
    
   }
}


In the second program below we will look into swapping of two numbers without using third variable. In the swap method our both the numbers i.e number1 and number2 are passed as arguments. In order to swap two numbers without using the third variable, we generally use mathematical logic. We add number1, number2 together and assign it to number1. After that in next step, we subtract number2 from number1 and assign it to number2. After that in next step, we subtract number2 from number1 and assign it to number1. Program to demonstrate how to swap two numbers without using third variable in Java
package com.hubberspot.swap;

/**
 *
 * @author jontymagicman
 */

public class Swap {
  public static void main(String[] args) {
    int number1 = 5;
    int number2 = 9;
    
    System.out.println("Before Swapping the numbers : ");
    System.out.println("Value of number1 is :" + number1);
    System.out.println("Value of number2 is :" +number2);
    
    swap(number1, number2);
  }
  
  private static void swap(int number1, int number2) {
    number1 = number1 + number2;
    number2 = number1 - number2;
    number1 = number1 - number2;
    
    System.out.println("After Swapping the numbers : ");
    System.out.println("Value of number1 is :" + number1);
    System.out.println("Value of number2 is :" +number2);
    
   }
}



Output of the programs : 
 
© 2021 Learn Java by Examples Template by Hubberspot