Hi ranchers,
prashanth kumar posted October 29, 2006 10:24 AM
I dont think that knowledge alone can help you fix this problem.
If you think so,than let me know how to apply it??
I just wanted to say, that operand precedence has not much to do with the problem - with one exception - but it is rather a problem of evaluation order.
The exception: here's one case of precedence.
+= has a lower rank than *
so, the multiplication is evaluated before the += operator:
k += ( (k = 4) * (k + 2) );
Then evaluation order comes into play:
Language specification: 15.7.1 Evaluate Left-Hand Operand First
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7.1 k += something or
k = k + something
something = ( (k = 4) * (k + 2) )
the k is taken stored into memory, and the result of "something" is added.
something = ( (k = 4) * (k + 2) )
from left to right again:
here the first expression is
(k=4)
that means that the variable k now becomes 4 (was 1).
further right:
.......... * (k+2)
k=4, from the step before, so (k+2) makes 6
(k=4) * 6
something = 4 * 6 = 24
and the whole thing:
k += 24
k was one when we started the from left to right tour, so
k= 1 + 24
the result is 25.
Another trick to visualize this is to mark where the k has what value. From left to right.
Now I'll quote it again:
I dont think that knowledge alone can help you fix this problem.
What else beside knowledge -
can ?
And what kind of knowledge you are thinking about? Knowledge about the K&B book, the language specification, arithmetics, boolean algebra?
I really don't know what you are driving at.
Yours,
Bu.
[ October 30, 2006: Message edited by: Burkhard Hassel ]