Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Vari
- {
- int a, b;
- void swap(Vari ob)
- {
- int c = ob.a;
- ob.a = ob.b;
- ob.b = c;
- System.out.print("\n\nIn swap by ref\n a = "+ ob.a + " b = "+ ob.b);
- }
- void swap(int a, int b)
- {
- int c = a;
- a = b;
- b = c;
- System.out.print("\n\nIn swap by value\n a = "+ a + " b = "+ b);
- }
- }
- public class val_ref {
- public static void main(String[] args)
- {
- Vari ob1 = new Vari();
- ob1.a = 10;
- ob1.b = 20;
- System.out.print("\nIn Main\n a = " + ob1.a + " b = " + ob1.b);
- ob1.swap(ob1.a, ob1.b);
- System.out.print("\n\nIn Main\n a = " + ob1.a + " b = " + ob1.b);
- ob1.swap(ob1);
- System.out.println("\n\nAgain, in Main\n a = " + ob1.a + " b = " + ob1.b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement