Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class L33PassbyRefvsPassbyVal{
- public static void main(String[] args){
- //Example 1: Pass by Val
- int i = 1;
- String s = "hello";
- boolean b = true;
- v(i, s, b);
- System.out.println(i);
- //prints 1
- System.out.println(s);
- //prints hello
- System.out.println(b);
- //prints true
- //Example 2: Pass by Ref (Need Circle class)
- // Circle c = new Circle(1);
- // int[] iArr = new int[5];
- // iArr[2] = 10;
- // r( c, iArr );
- // System.out.println(c.getRadius());
- // System.out.println(iArr[2]);
- //Example 3: Reassigning a Reference
- // int[] theArr = new int[5];
- // theArr[1] = 9;
- // theArr[2] = 5;
- // o( theArr );
- // System.out.println(theArr[1]);
- // System.out.println(theArr[2]);
- }
- public static void v(int i, String s, boolean b){
- i = 99;
- s = "hi";
- b = false;
- }
- public static void r(Circle someCircle, int[] anArr){
- someCircle.setRadius(20);
- anArr[2] = 99;
- }
- public static void o(int[] anArr){
- anArr[1] = 99; //Line 1
- anArr = new int[5]; //Line 2
- anArr[2] = 55; //Line 3
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement