In this tutorial learn how you can write a program to swap two numbers in java. For swapping the two numbers we are using a temporary variable. First, we store the value of the first number on temporary memory, and then we will store the second value on the first one and at last, we will store the temporary value to the second value.

temp = a;

a = b;

b = temp;

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.

Write a Program to Swap Two Numbers in Java

Here is the code to swap two numbers in Java programming.

//Write a program to multiply two floating-point numbers.
import java.util.Scanner;

public class Float {
    public static void main(String[] args) {
        float first_num, second_num, multiply;
        Scanner data = new Scanner(;
        System.out.println("Enter the first number:");
        first_num = data.nextFloat();
        System.out.println("Enter the second number:");
        second_num = data.nextFloat();
        multiply = first_num * second_num;
        System.out.println("Multiplication of two floating point number is: " + multiply + ".");


Test Live At Jdoodle


This is how you can write a program to swap two numbers in java programming. Comment below if you like our tutorial.