Java program to swap two numbers using function

Let’s learn java program to swap two numbers using function.

java program to swap two numbers using function

Java program to swap two numbers using function

In swapping of two numbers in java using function first we have to write swap function which swaps two numbers. Later this function is called in “main” method.

Also read – garbage collection in java

Here’s the java program to swap two numbers using function,

import java.util.Scanner;
// java swap function
public class SwapTwoNumberDemo 
{
   int numOne, numTwo;
   public  void swapNum(SwapTwoNumberDemo stn)
   {
      int temp;
      temp = stn.numOne;
      stn.numOne = stn.numTwo;
      stn.numTwo = temp;
   }
   public static void main(String[] args) 
   {
      SwapTwoNumberDemo obj = new SwapTwoNumberDemo();
      try
      {
         Scanner sc = new Scanner(System.in);             
         System.out.print("First  number : ");
         obj.numOne = sc.nextInt();

         System.out.print("Second number : ");
         obj.numTwo = sc.nextInt();

         obj.swapNum(obj);
         System.out.println("After swapping - numOne : " + obj.numOne + ", numTwo : " + obj.numTwo);
         sc.close();
      }
      catch(Exception ex)
      {
         System.out.println("Exception : " + ex.toString());
      }
   }
}



Output:

First number : 50
Second number : 100
After swapping – numOne : 100, numTwo : 50


Also read – swap two numbers in java without using third variable in java

Also read – how to swap two numbers in java using temporary variable


call by reference

In java we can call by value by passing reference variables to a method. Meanwhile objects are passed by call by reference.

Here’s swapping of two numbers in java using call by reference,

class IntegerWrapper
{
   int num1, num2;
   IntegerWrapper(int a, int b)
   {
      num1 = a;
      num2 = b;
   }
   void swapNumber(IntegerWrapper obj)
   {
      int n;
      n = obj.num1;
      obj.num1 = obj.num2;
      obj.num2 = n;
      System.out.println("Inside swap numbers method: ");
      System.out.println("Number 1 = " + obj.num1 + " Number 2 = " + obj.num2);
   }
}

public class SwappingCallByReference 
{
   public static void main(String[] args) 
   {
      IntegerWrapper obj = new IntegerWrapper(60, 90);
      System.out.println("Before swapping two numbers: ");
      System.out.println("Number 1 = " + obj.num1 + " Number 2 = " + obj.num2);
      obj.swapNumber(obj);
      System.out.println("After swapping two numbers: ");
      System.out.println("Number 1 = " + obj.num1 + " Number 2 = " + obj.num2);
   }
}


Output:

Before swapping two numbers: Number 1 = 60 Number 2 = 90
Inside swap numbers method: Number 1 = 90 Number 2 = 60
After swapping two numbers: Number 1 = 90 Number 2 = 60

Swap three numbers with using temp variable in java

Let’s learn java program on swapping of three numbers in java with using temp variable,

import java.util.Scanner;

public class SwapThreeNumbers 
{
   public static void main(String[] args) 
   {
      int num1, num2, num3, temp;   
      System.out.println("Please enter first number: ");
      Scanner sc = new Scanner(System.in);
      num1 = sc.nextInt();
      System.out.println("Please enter second number: ");
      num2 = sc.nextInt();
      System.out.println("Please enter third number: ");
      num3 = sc.nextInt();
      System.out.println("Before swapping first number is: " + num1);
      System.out.println("Before swapping second number is: " + num2);
      System.out.println("Before swapping third number is: " + num3);
      temp = num1;
      num1 = num2;
      num2 = num3;
      num3 = temp;
      System.out.println("---------------------------------------");
      System.out.println("After swapping first number is: " + num1);
      System.out.println("After swapping second number is: " + num2);
      System.out.println("After swapping third number is: " + num3);
      sc.close();
   }
}


Output:

Please enter first number: 30
Please enter second number: 60
Please enter third number: 90
Before swapping first number is: 30
Before swapping second number is: 60

Before swapping third number is: 90

After swapping first number is: 60
After swapping second number is: 90
After swapping third number is: 30

Swapping of three numbers in java without temporary variable

Now let’s learn java program to swap three numbers without using temporary variable or swap three numbers without using third variable in java,

public class SwapThreeNumbersWithoutTemp
{
   static int num1, num2, num3;
 
   public static void main(String[] args) 
   {
      num1 = 30; num2 = 60; num3 = 90; 
      System.out.println("Before swapping three numbers: num1 = " + num1 + ", num2 = " + num2 + ", num3 = " + num3);
      swapWithoutTemporary();
      System.out.println("After swapping three numbers: num1 = " + num1 + ", num2 = " + num2 + ", num3 = " + num3);
   }

   static void swapWithoutTemporary() 
   { 
      num1 = num1 + num2 + num3; 
      num2 = num1 - (num2 + num3); 
      num3 = num1 - (num2 + num3); 
      num1 = num1 - (num2 + num3);  
   }
}


Output:

Before swapping three numbers: num1 = 30, num2 = 60, num3 = 90
After swapping three numbers: num1 = 90, num2 = 30, num3 = 60