Advertisement
fosterbl

L33PassbyRefvsPassbyVal.java

Dec 17th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. public class L33PassbyRefvsPassbyVal{
  2.      
  3.    public static void main(String[] args){
  4.       //Example 1: Pass by Val
  5.       int i = 1;
  6.       String s = "hello";
  7.       boolean b = true;
  8.       v(i, s, b);
  9.       System.out.println(i);
  10.       //prints 1
  11.       System.out.println(s);
  12.       //prints hello
  13.       System.out.println(b);
  14.       //prints true
  15.      
  16.       //Example 2: Pass by Ref (Need Circle class)
  17. //       Circle c = new Circle(1);
  18. //       int[] iArr = new int[5];
  19. //       iArr[2] = 10;
  20. //       r( c, iArr );
  21. //       System.out.println(c.getRadius());
  22. //       System.out.println(iArr[2]);
  23.    
  24.       //Example 3: Reassigning a Reference
  25. //       int[] theArr = new int[5];
  26. //       theArr[1] = 9;
  27. //       theArr[2] = 5;
  28. //       o( theArr );
  29. //       System.out.println(theArr[1]);
  30. //       System.out.println(theArr[2]);
  31.    
  32.    
  33.    }
  34.    
  35.    public static void v(int i, String s, boolean b){
  36.       i = 99;
  37.       s = "hi";
  38.       b = false;
  39.    }
  40.    
  41.    public static void r(Circle someCircle, int[] anArr){
  42.       someCircle.setRadius(20);
  43.       anArr[2] = 99;
  44.    }
  45.    
  46.    public static void o(int[] anArr){
  47.      anArr[1] = 99;        //Line 1
  48.      anArr = new int[5];   //Line 2
  49.      anArr[2] = 55;        //Line 3
  50.    }
  51.  
  52.  
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement