I have a question,
In Java
, does Math.min
bind tighter than ++
?
Let me illustrate with an example and maybe someone can explain to me why I get the results I get.
Here's a method I run:
private static void testIncrement() {
int x=10;
System.out.println(x++);
System.out.println(x);
x=10;
System.out.println("-----------");
System.out.println(++x);
System.out.println(x);
x=10;
System.out.println("-----------\n"+x); //10
x=Math.min(255, x++);
System.out.println(x); **//x=10 WHY NOT x=11?**
x=10;
System.out.println("-----------\n"+x);
x=Math.min(255, ++x);
System.out.println(x);
}
The results are:
10
11
-----------
11
11
-----------
10
10
-----------
10
11
On the line where I put //x=10 WHY NOT x=11?
I wonder why x
is 10 and not 11. Maybe someone can explain this to me.
It looks as if Math.min
create a copy of x
(which is 10 at this time) which it uses to do Math.min
. Then the original x
is incremented from 10 to 11, but the copy which is still 10 comes out of Math.min
and overwrites the incremented one.
Does this make sense? Does anyone have an explanation for why x is 10 and not 11 in this case?
Thanks
PS - I totally understand How do the post increment (i++) and pre increment (++i) operators work in Java?