Advertisement
AnindyaBiswas

val_ref

Jun 2nd, 2022
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Vari
  4. {
  5.     int a, b;
  6.    
  7.     void swap(Vari ob)
  8.     {
  9.         int c = ob.a;
  10.         ob.a = ob.b;
  11.         ob.b = c;
  12.         System.out.print("\n\nIn swap by ref\n a = "+ ob.a + " b = "+ ob.b);
  13.     }
  14.    
  15.     void swap(int a, int b)
  16.     {
  17.         int c = a;
  18.         a = b;
  19.         b = c;
  20.         System.out.print("\n\nIn swap by value\n a = "+ a + " b = "+ b);
  21.     }
  22. }
  23.  
  24.  
  25.  
  26. public class val_ref {
  27.  
  28.  
  29.     public static void main(String[] args)
  30.     {
  31.        
  32.         Vari ob1 = new Vari();
  33.         ob1.a = 10;
  34.         ob1.b = 20;
  35.         System.out.print("\nIn Main\n a = " + ob1.a + " b = " + ob1.b);
  36.         ob1.swap(ob1.a, ob1.b);
  37.         System.out.print("\n\nIn Main\n a = " + ob1.a + " b = " + ob1.b);
  38.         ob1.swap(ob1);
  39.        
  40.         System.out.println("\n\nAgain, in Main\n a = " + ob1.a + " b = " + ob1.b);
  41.  
  42.     }
  43.    
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement