1import java.util.Scanner;
2// java swap function
3public class SwapTwoNumberDemo
4{
5 int numOne, numTwo;
6 public void swapNum(SwapTwoNumberDemo stn)
7 {
8 int temp;
9 temp = stn.numOne;
10 stn.numOne = stn.numTwo;
11 stn.numTwo = temp;
12 }
13 public static void main(String[] args)
14 {
15 SwapTwoNumberDemo obj = new SwapTwoNumberDemo();
16 try
17 {
18 Scanner sc = new Scanner(System.in);
19 System.out.println("First number : ");
20 obj.numOne = sc.nextInt();
21 System.out.println("Second number : ");
22 obj.numTwo = sc.nextInt();
23 obj.swapNum(obj);
24 System.out.println("After swapping - numOne : " + obj.numOne + ", numTwo : " + obj.numTwo);
25 sc.close();
26 }
27 catch(Exception ex)
28 {
29 System.out.println("Exception: " + ex.toString());
30 }
31 }
32}
1int a, b, c;
2a = 5;
3b = 3;
4System.out.println("Before SWAP a = " + a + ", b = " + b);
5c = a;
6a = b;
7b = c;
8System.out.println("After SWAP a = " + a + ", b = " + b);
1int a = 5;
2int b = 6;
3
4int temp = a;
5a = b;
6b = temp;
7
8// now a is 6 and b is 5.
9