Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Operator precedence questions in SCJP1.5

prashanth kumar
Ranch Hand
Posts: 162
Hello All,
In the enthuware mock tests which iam doing,iam encountering too many precedence related questions..But K and B explicitely mentions that only basic precedence stuff is more than necessary[like *,/ and % has more precedence than +,-]..can anyone comment on this..Any views from people who have cleared the exam is very much welcome..

Heres one of the question from that mock...

best regards
prashanth

Burkhard Hassel
Ranch Hand
Posts: 1274
Hi,
I find this is more about "evaluate expressions from left to right" than precedence of operators. Would be different, if you remove all the brackets.
And even then it was no more than + and * and +=
Bu.

prashanth kumar
Ranch Hand
Posts: 162
Hello Bu,
I dont think that knowledge alone can help you fix this problem.
If you think so,than let me know how to apply it??

Many Thanks
Prashanth

wise owen
Ranch Hand
Posts: 2023

Atul Shukla
Ranch Hand
Posts: 34
Above Limk is very helpful, thanks Wise!!

Burkhard Hassel
Ranch Hand
Posts: 1274
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 ]

prashanth kumar
Ranch Hand
Posts: 162
Hello Bu,
Thanks for the great explanation..
I was talking about K and B book as i mentioned in my first post itself..[Page 278 of book...given under exam watch]..
Seems like i need to study bit more about operator precedence.

Thanks again for the explanation
Prashanth

Bert Bates
author
Sheriff
Posts: 8900
5
Hey Prashanth,

Up until the 5.0 exam Sun paid a lot of attention to operator precedence on the exam. As of the 5.0 exam the weighting of this topic (precedence) was greatly reduced and simplified. My advice is to spend your time on the tricky topics that get more weight. So, for instance, make sure that you've got a really solid understanding of threads, garbage collection, generics, collections, and classpaths, before you spend a lot of time on precedence. You're almost certainly going to get more questions on these topics than you will on precedence. If you've got everything else nailed, then certainly have fun studying precedence.

hth,

Bert

prashanth kumar
Ranch Hand
Posts: 162
Thanks very much Bates..

Its Much appreciated

Prashanth