1

Given the code below:

public class A {

   private int i = 6;
   private int j = i;

   public A() {
       i = 5;
   }

   public static void main(String[] args) {
      A a = new A();
      System.out.println(a.i + a.j);
   }
}

Why is the output of this addition is 11 and not 10? From what I understand, the constructor runs last after static and instance variables are initialised. Therefore if the constructor is the last to be triggered it should be setting "both" i and j to 5 since they point to the same number. Could someone please explain step by step what is happening here so that once executed the value is 11. Any feedback would be greatly appreciated.

CBA110
  • 1,072
  • 2
  • 18
  • 37

4 Answers4

4

since they point to the same number

There are no pointers in Java.

i and j are primitive variables holding integer values.

int j = i;

assigns the current value of j to i. Changing i after the assignment doesn't change j.

Therefore i is first initialized to 6, then j is assigned 6, and then i is changed to 5 (which doesn't change the value of j).

Eran
  • 387,369
  • 54
  • 702
  • 768
  • So the reason the output is 11 is because these are primitives and not objects? If they were objects surely the references would be addressing the same object in memory and object j would be the same as i ((j == i) would be true). – CBA110 Jan 18 '17 at 14:21
  • 1
    @CBA110 No. The answer would be the same if you used `Integer` instead of `int`. When you assign a new value to a variable (whether it's a primitive value or a reference to an object), you are not affecting the value of other variables that happen to have the same value prior to the assignment. If, on the other hand, `i` and `j` both refer to the same mutable object, calling `i.setSomething(value)` would also change the state of the object referenced by `j`, since both would be referring to the same object. – Eran Jan 18 '17 at 14:26
  • Perfect. Thank you @Eran. – CBA110 Jan 18 '17 at 14:32
2

What you're describing applies to a reactive programming model. The JVM does not work that way. The j variable contains the old value of i at the time of its assignment in the field declaration. Hence its value remains 6. After the value of i changes, the new value is not published to j.

M A
  • 71,713
  • 13
  • 134
  • 174
0

Everything is a pointer in Java, it just hides it to you. That can lead to confusion when you come from other languages.

private int i = 6; // i is an int pointer, the value hold at his address is 6
private int j = i; // j is an int pointer, its value is assigned from the value hold at i, therefore it is 6
A a = new A(); // i is now assigned a value of 5, but i and j have different address, so j isn't impacted.

If you wanted such behavior as you described, you would have to use an object.

private int[] i = new int[] {6}; // Not sure of this syntax
private int[] j = i;
i[0] = 5;
System.out.println(i[0]+j[0]);

There it is.

Adrien Horgnies
  • 691
  • 4
  • 11
0

Flow of execution is:-

private int i = 6;
private int j = i;

These two statements are executed at first so the values of i and j are 6. Now when you call the constructor then you get i=5 . Value of i is now changed but value of j is still 6.

so when you say a.i+a.j then it is 5+6 and hence 11 is the answer. Changes in i won't change j as they are of primitive data type and objects.

Ajinkya Patil
  • 741
  • 1
  • 6
  • 17