0
class Box {
    int size;

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

public class Laser {
    public static void main(String[] args) {
        Box b1 = new Box(5);
        Box[] ba = go(b1, new Box(6));
        ba[0] = b1;
        for (Box b : ba)
            System.out.print(b.size + " ");
    }

    static Box[] go(Box b1, Box b2) {
        b1.size = 4;
        Box[] ma = { b2, b1 };
        return ma;
    }
}

What the result? 
A. 4 4 
B. 5 4 
C. 6 4 
D. 4 5 
E. 5 5 
F. Compilation fails 

The answer is A

I am having hard time trying to understand the results of this code. Can anyone explain the results to me?

Razib
  • 10,965
  • 11
  • 53
  • 80
Programmer345
  • 540
  • 2
  • 15
  • 29

4 Answers4

3

In Java it is always pass by value

Primitives are always pass by value

Objects are passed by their references and those references are also passed by value.

    public static void main(String[] args) {
        Box b1 = new Box(5); //Line1
        Box[] ba = go(b1, new Box(6)); //Line2
        ba[0] = b1; //Line3
        for (Box b : ba)
            System.out.print(b.size + " ");
    }

The answer is (4 4) because,

  • In line1 you're initializing the size of Box to 5 inside the constructor.
  • In line 2 you're sending two objects as arguments to the method go(). The size of object referred by b1 is now 5 and the new Box(6); object has size 6. Inside the method, you're changing the value of 1st parameter from 5 to 4. And finally you returned array ma with b2 as first element and b1 as second element. b2 has value 6 and b1 has 4.
  • Now in the line 3 you're again changing the value of first element inside the array. You replaced the first element inside the array b2 which has a value 6 with b1 whose value now is 4.
Prudhvi
  • 2,276
  • 7
  • 34
  • 54
3

Here is some explanations -

1. First of all you have create a Box in main() class -

 Box b1 = new Box(5);  

2. Then send b1 to go() method -

   Box[] ba = go(b1, new Bo(6));

Now b1.size is 5

3. In method go() you are setting b1.size to 4 using the assignment statement -

b1.size = 4;  

4. Now you are creating a new Box array ma -

Box[] ma = { b2, b1 };  

In this array the first element is b2 and b2.size is 6. See point 2.

5. Then you return ma and at point 2 the returned array ma is assigned with ba. Now the first element of the array is 6 and the second element of the array is 4.

6. ba[0] = b1; - this statement sets the first element of ba[0] to b1. Note at point 2 b1.size was set to 4.

Now ba[0].size is 4
and ba[1].size is 4

And that's why the output is 4 4

Razib
  • 10,965
  • 11
  • 53
  • 80
  • 1
    In Point 1 in Box b1 = new Box(5); are we creating a variable or an array of length 5 ? – Programmer345 Mar 24 '15 at 21:29
  • We are creating a new variable `b1` of type `Box`. In java we create array like as follows: `int[] intArray = new int(5)`. – Razib Mar 24 '15 at 21:39
  • So does Box b1 = new Box(5); means that we are creating variable of length 5 or value 5 ? Is that the same thing like saying b1 = 5 ? – Programmer345 Mar 24 '15 at 21:44
  • No it's not the same of b1 = 5. Here `b1` is of type `Box` and 5 is int. So we can not assign `b1` to 5. It would be a wrong assignment. The `new Box(5)` create a new object of type `Box` using the constructor `Box(int size)`. After that the newly created object of `Box` with size 5 is assigned with `b1`. – Razib Mar 24 '15 at 21:50
2

When Box[ ] ba = go (b1, new Bo(6)); is run, go instantiates ba with the following array: [b2,b1], where b2 has a size of 6 and b1 has a size of 4. To be clear, go sets the size of b1 from 5 to 4. When b1, is passed to go, it is passed by reference, which means any changes to b1 inside go are also changes made to the b1 object that was created in the first line of main.

Then the following code is run:

ba[0] = b1;

Here, you're setting b2 from within the array(since ba[0] refers to b2) equal to b1. Now, both b2 and b1 refer to a box with a size of 4.

P.S. Fun fact, objects never directly equal data; objects are only addresses which correspond to or refer to data written elsewhere on the stack. So, technically, when ba[0] = b1; is run, b2 is simply referring to the same data that b1 is referring to.

hmir
  • 1,333
  • 2
  • 17
  • 24
0

In java all the primitive types are passed by value while the rest, like Objects or arrays are passed by reference. It means that when you call the method

go(Box b1, Box b2)

it changes the value of b1.size everywhere because it sets b1.size = 4, so the result is A: 4 4

Chris
  • 1,692
  • 2
  • 17
  • 21
  • "Objects or Arrays are passed by reference and those references are passed by value". This would be more correct IMO. – Prudhvi Mar 24 '15 at 21:24
  • Yep you're right. What i wanted to say is that all changes to an object are reflected in the whole program – Chris Mar 24 '15 at 21:37