This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator precedence questions in SCJP1.5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operator precedence questions in SCJP1.5" Watch "Operator precedence questions in SCJP1.5" New topic
Author

Operator precedence questions in SCJP1.5

prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
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


SCJP1.5(86%)<br />SCWCD1.4(95%)<br />SCBCD1.3(92%)<br />IBM 252
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
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.


all events occur in real time
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
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

Joined: Feb 02, 2006
Posts: 2023
What are operator precedence and the order of operand evaluation?
Atul Shukla
Ranch Hand

Joined: Oct 09, 2006
Posts: 34
Above Limk is very helpful, thanks Wise!!
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
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

Joined: Mar 22, 2004
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

Joined: Oct 14, 2002
Posts: 8803
    
    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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Thanks very much Bates..

Its Much appreciated

Prashanth
 
Consider Paul's rocket mass heater.
 
subject: Operator precedence questions in SCJP1.5
 
Similar Threads
Q on post increment: JLS explanation
Order of operations with operators
Q's On My Exam Prep
Operators - Mughal p.56 Ques 3.7
Parentheses in Expression