Java Swapping Methods

  1. Int Value Swap in Java
  2. Float Value Swap in Java
  3. String Value Swap in Java
  4. Swap method without using third variable

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Int Value Swap in Java


Swap_Ex1.java

import java.util.Scanner;
 
public class Swap_Ex1 {
	
	int a, b, temp;
	Scanner scan;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Integer Swapping method\n");
		
		System.out.println("Enter first value 'a' : ");
		a = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter second value 'b' : ");
		b = Integer.parseInt(scan.nextLine());
	}
	
	void swap() {
		
		System.out.println("\nBefore Swapping\na : " + a + " & b : " + b);
		
		temp = a;
		a = b;
		b = temp;
		
		System.out.println("\nAfter Swapping\na : " + a + " & b : " + b);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Swap_Ex1 obj = new Swap_Ex1();
		
		obj.getVal();
		obj.swap();
	}
}

Sample Output

Integer Swapping method

Enter first value 'a' : 
10
Enter second value 'b' : 
20

Before Swapping
a : 10 & b : 20

After Swapping
a : 20 & b : 10




B. Float Value Swap in Java


Swap_Ex2.java

import java.util.Scanner;
 
public class Swap_Ex2 {
	
	float a, b, temp;
	Scanner scan;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Integer Swapping method\n");
		
		System.out.println("Enter first value 'a' : ");
		a = Float.parseFloat(scan.nextLine());
		
		System.out.println("Enter second value 'b' : ");
		b = Float.parseFloat(scan.nextLine());
	}
	
	void swap() {
		
		System.out.println("\nBefore Swapping\na : " + a + " & b : " + b);
		
		temp = a;
		a = b;
		b = temp;
		
		System.out.println("\nAfter Swapping\na : " + a + " & b : " + b);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Swap_Ex2 obj = new Swap_Ex2();
		
		obj.getVal();
		obj.swap();
	}
}

Sample Output

Integer Swapping method

Enter first value 'a' : 
10.50
Enter second value 'b' : 
20.60

Before Swapping
a : 10.5 & b : 20.6

After Swapping
a : 20.6 & b : 10.5




C. String Value Swap in Java


Swap_Ex3.java

import java.util.Scanner;
 
public class Swap_Ex3 {
	
	String a, b, temp;
	Scanner scan;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Integer Swapping method\n");
		
		System.out.println("Enter first value 'a' : ");
		a = scan.nextLine();
		
		System.out.println("Enter second value 'b' : ");
		b = scan.nextLine();
	}
	
	void swap() {
		
		System.out.println("\nBefore Swapping\na : " + a + " & b : " + b);
		
		temp = a;
		a = b;
		b = temp;
		
		System.out.println("\nAfter Swapping\na : " + a + " & b : " + b);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Swap_Ex3 obj = new Swap_Ex3();
		
		obj.getVal();
		obj.swap();
	}
}

Sample Output

Integer Swapping method

Enter first value 'a' : 
Balaji
Enter second value 'b' : 
Samplecodez

Before Swapping
a : Balaji & b : Samplecodez

After Swapping
a : Samplecodez & b : Balaji




D. Swap method without using third variable


Swap_Ex4.java

import java.util.Scanner;
 
public class Swap_Ex4 {
	
	int a, b, temp;
	Scanner scan;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Integer Swapping method\n");
		
		System.out.println("Enter first value 'a' : ");
		a = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter second value 'b' : ");
		b = Integer.parseInt(scan.nextLine());
	}
	
	void swap() {
		
		System.out.println("\nBefore Swapping\na : " + a + " & b : " + b);
		
		a = a + b;
		b = a - b;
		a = a - b;
		
		System.out.println("\nAfter Swapping\na : " + a + " & b : " + b);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Swap_Ex4 obj = new Swap_Ex4();
		
		obj.getVal();
		obj.swap();
	}
}

Sample Output

Integer Swapping method

Enter first value 'a' : 
11
Enter second value 'b' : 
20

Before Swapping
a : 11 & b : 20

After Swapping
a : 20 & b : 11






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com