In this quick article, we will learn two techniques to swap two numbers in Java. The first one uses a temporary variable for swapping, while the second one doesn't use any temporary variables.
1. Java program to swap two numbers using a temporary variable
package com.javaguides.java.tutorial;
import java.util.Scanner;
/**
* Java Program to Swap Two Numbers
*
* @author https://www.javaguides.net/
*
*/
public class JavaProgram {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter first number:");
int first = scanner.nextInt();
System.out.print("Enter second number:");
int second = scanner.nextInt();
System.out.println("--Before swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
// Value of first is assigned to temporary
int temporary = first;
// Value of second is assigned to first
first = second;
// Value of temporary (which contains the initial value of first) is assigned to
// second
second = temporary;
System.out.println("--After swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
}
Output:
Enter first number:10
Enter second number:20
--Before swap--
First number = 10
Second number = 20
--After swap--
First number = 20
Second number = 10
2. Java program to swap two numbers without using a temporary variable
Let's rewrite the above Java program to swap two numbers without using a temporary variable:
package com.javaguides.java.tutorial;
import java.util.Scanner;
/**
* Java Program to Swap Two Numbers
*
* @author https://www.sourcecodeexamples.net/
*
*/
public class JavaProgram {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter first number:");
int first = scanner.nextInt();
System.out.print("Enter second number:");
int second = scanner.nextInt();
System.out.println("--Before swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
first = first - second;
second = first + second;
first = second - first;
System.out.println("--After swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
}
Output:
Enter first number:10
Enter second number:20
--Before swap--
First number = 10
Second number = 20
--After swap--
First number = 20
Second number = 10
Related Java Programs
- String Programs in Java with Output
- Java Program to Count Number of Duplicate Words in String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains only Digits
- How to Check if the String Contains only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Check if Input String is Palindrome
- Java Program to Find all Permutations of String
- How to Remove or Trim All White Spaces from a String in Java
- How to Remove Leading and Trailing White Space From a String in Java
- Java Program to Count Duplicate Characters in a String
- Remove Character from String in Java (Java 8)
- Java Program to Count Vowels and Consonants in a String (Java 8)
- 4 Ways to Find First Non-Repeated Character in String in Java
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course