int s = 20, t = 15; S+ = 5: --t: What are the values of s and t
Answers
Answer:
int s = 20;
int t = s++ + --s;
Working: increment(post increment) s to 21 (current value 20) + decrement(pre decrement) s to 20 (current value 20).
So, t=20+20;
And s =20;
After taking into account precedence and parentheses, Java guarantees that expressions will be evaluated left to right. For example, to evaluate eat() + drink() - beMerry(), Java will first evaluate eat(), then drink(), then perform the addition, then evaluate beMerry(), and finally perform the subtraction. eat() is evaluated before drink(), because eat() is to the left of drink(), and expressions are evaluated left to right. This guarantee is important because the invocations of eat() and drink() may have side effects that would differ if they were invoked in the opposite order.
Explanation:
Since the increment/decrement operator modifies its operand, use of such an operand more than once within the same expression can produce undefined results. For example, in expressions such as x − ++x, it is not clear in what sequence the subtraction and increment operators should be performed. Situations like this are made even worse when optimizations are applied by the compiler, which could result in the order of execution of the operations to be different than what the programmer intended.