This might seem simple but it’s just stumbled me and my friends…
lets take the following piece of code-
in java
//........
int a=10;
a= a-- + a--;
System.out.print("a="+a);
//........
in c
//........
int a=10;
a= a-- + a--;
printf("a= %d",a);
//.......
where in the former case you get output as 19 in C you get it as 18.
the logic in c is understandable but in java?
in java if its like
int a=10;
a=a++;
in this case the output is 10.
So what’s the logic?
a = a-- + a--causes undefined behaviour in C. C does not define which decrement should be evaluated first.a--evaluates to the value of a, and after that it decrements a,so in Java
a = a-- + a--evaluates like this:a = (10, decrement a) + (9, decrement a)
The second operand is 9 because first term caused a to be decremented.
In summary: With that expression, C does not define the evaluation order. Java defines it to be from left to right.