Share your repls and programming experiences

← Back to all posts
Swapping of two numbers in java with temporary variable
RaghunathThilag (1)

Swapping of two numbers in java with temporary variable for beginners to understand the concept of swapping program in java. The temp method in the Java programming language is used to swapping the numbers in a program. Swap indicates that the data will be changed to the user-initiated data.

public class Main
{
public static void main(String[] args)
{
float a = 3.66f, b = 6.47f;
System.out.println("a number " + a);
System.out.println("b number " + b);
float temp = a;
a = b;
b = temp;
System.out.println("After swapping");
System.out.println("a number " + a);
System.out.println("b number " + b);
}
}

Output
a number 3.66
b number 6.47
After swapping
a number 6.47
b number 3.66

Link : https://inlarn.com/swapping-of-two-numbers-in-java-with-temporary-variable/