Java How To Swap Two Variables
Swap Two Variables
Exchange the values of two variables using a temporary variable:
Example
int a = 5;
int b = 10;
int temp = a;
a = b;
b = temp;
System.out.println("a = " + a + ", b = " + b);
Explanation: We save a
in a temporary variable, move b
into a
, and then move the saved value into b
.