package sec06.exam03;
public class Exercise02 {
public static void main(String[] args) {
int x = 10;
int y = 20;
int z = (++x) + (y--);
System.out.println(z);//30
}
}
Isn't it normal when the answer is 30? I don't understand why 31. The value of ++x is 11 and the value of y--isn't it 19? I think y=y-1; if I were to write y-- I wonder why 31.
calculate
It depends on what you calculate first. In the case of x++, increase or decrease x by 1. For ++x, increase or decrease x by 1. Now, if both cases are the same, we don't have to use two subtly different notation, do we? Maybe if it was Babu who created the programming language. Well, I don't think it's Babu, and it's not really... The order of calculations for z = ++x is (2)z = (1)x+1, i.e. x+1 is calculated first and stored in z. The order of calculations for z = x++ is (1)z = x (2) x = x+1.
public static void main(String[] args) {
int x = 10;
int y = 20;
int z = (++x) + (y--);
It's /*y-- right? In the operations of this line, y-1 is the result of all operations
After it's over. I'm going to be the last one. It's tied up with ()
I think y-- is going to precede the = operator, but it's not, right?
I didn't know either. But that's a language rule.
It's just that the creator of java promised to do so.
To keep the language consistent!
So even the parenthesis operator is pushed back to the last minute!*/
System.out.println(z);//30
}
The operations differ depending on the increase and decrease operator (++, ---) symbol followed by the latter.
z = (++x) + (y--) // 31
z = (++x) + (--y) // 30
You can think of an expression as having a value. So you think that things like "++x" and "x++" are all worth it. The contents are summarized as follows.
x++ // x
++x // x+1
x-- // x
--x // x-1
© 2024 OneMinuteCode. All rights reserved.