Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+22 votes

int x;
x = 3;
int y = x;
x = 5;
y = y + x;
System.out.println(x);
System.out.println(y);

The output is:
5
8

asked in CSC211_Winter2018 by (1 point)

1 Answer

+10 votes

This is because of the order in which java reads the code. So, y = x when x = 3. After this line x is then changed to 5; however, y is still 3 because that is what x was when y = x.

If the statement "y = x;" would have come after "x = 5;" then y would equal 5.

In other words, the statement "y = x;" in java is not the same as saying y will always be equal to x. y is merely set to whatever value x currently holds.

answered by (1 point)
+5

Yes, it might help if you read "=" as "is assigned the value of", instead of "equals".

+4

Thank you, it helps a lot !!

...