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

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

java program to swap two numbers using function

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.println("First number : ");
         obj.numOne = sc.nextInt();
         System.out.println("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


swapping of two numbers in java using call by reference

In java objects are passed by call by reference.

Also read – garbage collection in java

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

class Demo
{
   int num1, num2;
   Demo(int a, int b)
   {
      num1 = a;
      num2 = b;
   }
   void swapNumber(Demo 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) 
   {
      Demo obj = new Demo(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


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


swapping of two numbers in one statement in java

To swap two integers in single line or in one statement we are using bitwise xor operator.

Bitwise xor operator of two numbers returns 1 if both bits in its operand are different else returns 0.

Let’s see an example on swapping of two numbers in one statement in java.

public class SwappingInOneStatement
{
   public static void main(String[] args)
   {
      int x = 23;
      int y = 75;
      System.out.println("Before swapping two numbers: x = " + x + " y = " + y);
      x = x ^ y ^ (y = x);
      System.out.println("After swapping two numbers: x = " + x + " y = " + y);
   }
}


Output:

Before swapping two numbers: x = 23 y = 75
After swapping two numbers: x = 75 y = 23