Example: #1 - Swapping of two numbers using temporary variable
Java Input Screen
class SwapExample {
public static void main(String[] args) {
int a = 3;
int b = 6;
System.out.println("The value of a before swap is " + a);
System.out.println("The value of b before swap is " + b);
// Swapping of two numbers using temp variable
int temp = a;
a = b;
b = temp;
System.out.println("The value of a after swap is " + a);
System.out.println("The value of b after swap is " + b);
}
}
Java Output Screen
The value of a before swap is 3
The value of b before swap is 6
The value of a after swap is 6
The value of b after swap is 3