1/*call by refrence doesnot change the value of parameters(a and b ) in
2main function but where as passing the values by refrence changes the values
3of and b in the main function
4*/
5//CALL BY REFERENCE
6public class JavaTester {
7 public static void main(String[] args) {
8 IntWrapper a = new IntWrapper(30);
9 IntWrapper b = new IntWrapper(45);
10 System.out.println("Before swapping, a = " + a.a + " and b = " + b.a);
11 // Invoke the swap method
12 swapFunction(a, b);
13 System.out.println("\n**Now, Before and After swapping values will be different here**:");
14 System.out.println("After swapping, a = " + a.a + " and b is " + b.a);
15 }
16 public static void swapFunction(IntWrapper a, IntWrapper b) {
17 System.out.println("Before swapping(Inside), a = " + a.a + " b = " + b.a);
18 // Swap n1 with n2
19 IntWrapper c = new IntWrapper(a.a);
20 a.a = b.a;
21 b.a = c.a;
22 System.out.println("After swapping(Inside), a = " + a.a + " b = " + b.a);
23 }
24}
25class IntWrapper {
26 public int a;
27 public IntWrapper(int a){ this.a = a;}
28}
29/*
30------------------------------------OUTPUT--------------------------------------
31Before swapping, a = 30 and b = 45
32Before swapping(Inside), a = 30 b = 45
33After swapping(Inside), a = 45 b = 30
34**Now, Before and After swapping values will be different here**:
35After swapping, a = 45 and b is 30
36--------------------------------------------------------------------------------
37*/
38//CALL BY VALUE
39public class Tester{
40 public static void main(String[] args){
41 int a = 30;
42 int b = 45;
43 System.out.println("Before swapping, a = " + a + " and b = " + b);
44 // Invoke the swap method
45 swapFunction(a, b);
46 System.out.println("\n**Now, Before and After swapping values will be same here**:");
47 System.out.println("After swapping, a = " + a + " and b is " + b);
48 }
49 public static void swapFunction(int a, int b) {
50 System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
51 // Swap n1 with n2
52 int c = a;
53 a = b;
54 b = c;
55 System.out.println("After swapping(Inside), a = " + a + " b = " + b);
56 }
57}
58/*
59-----------------------------------OUTPUT---------------------------------------
60Before swapping, a = 30 and b = 45
61Before swapping(Inside), a = 30 b = 45
62After swapping(Inside), a = 45 b = 30
63**Now, Before and After swapping values will be same here**:
64After swapping, a = 30 and b is 45
65--------------------------------------------------------------------------------
66*/
67
68
69
70
71
72