Swapping of Two Numbers in Java With Temporary Variable

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

Temp Variable in Java

The term “temporary variables” is not defined in Java. This is basically a catch-all phrase for items that are made and then swiftly discarded, having an extremely short lifespan. The notion is applicable to a wide range of languages. In most languages, the criteria for determining how long a variable exists are similar.

Float Variable in Java

The float keyword in Java refers to a basic data type. It’s a 32-bit IEEE 754 floating-point with single precision. It’s where variables and methods are declared. It is used to represent fractional numbers. A variable of the floating-point type may contain a real value, such as 4320.0, -3.33, or 0.01226. The floating portion of the term alludes to the decimal point’s ability to “float,” that is, to accommodate a variable number of digits before and after the decimal point.

Swapping of Two Numbers in Java

The term “swapping two variables” refers to the exchange of the variables’ values. This is usually done with data stored in memory. Using a third temporary variable to swap two variables is the easiest technique.

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);


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