Swapping of two numbers in java with temporary variable

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 

What is temporary variable in java and why it doesnt require declaration

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.

What is float variable in java and how to declare float

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 with temporary variable

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.