0

this code segment is an alteration to a question in the oca 7 study guide. dealing with array and arrayList.
I am trying to wrap my head around how the object Box b1 is altered by the go method.

class Box {
    int size;
    Box (int s){size = s;}
}

public class Laser {

    public static void main(String[] args) {
        Box b1 = new Box(5);
        System.out.println("this is b1 at line 20" + " = " +b1.size);
        Box[] ba = go(b1, new Box(6));
        System.out.println("this is b1 at line 22" + " = " +b1.size);
        ba[0] = b1;
        for(Box b : ba) System.out.print(b.size + " ");
    }
    static Box [] go(Box b3, Box b4){
        b3.size=4;
        Box[] ma = {b4,b3};
        return ma;
   }

}

The output is

this is b1 at line 20 = 5

this is b1 at line 22 = 4

4 4

I expected the output to be

this is b1 at line 20 = 5

this is b1 at line 22 = 5

5 4

Claire Nagle
  • 69
  • 1
  • 6
  • 1
    If the question used to mark this one as duplicate doesn't help.. contact us again :) – TheLostMind Jun 11 '15 at 06:24
  • Did you expect `b1` to get copied when passed to `go()`? It isn't, a reference to `b1` is passed (named `b3` inside `go()`). – dhke Jun 11 '15 at 06:26
  • I did expect only the value to get passed. after reading the the linked question. I remember the pointer issue. and it is not the value that is passed but where to find the data. – Claire Nagle Jun 11 '15 at 06:44

0 Answers0