Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator Precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operator Precedence" Watch "Operator Precedence" New topic

Operator Precedence

Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
The following question is taken from Dan Chisholm's prep exam:

What is the result of attempting to compile and run the above program?
a. Prints: 1,2,3,4,5,1
b. Prints: 1,2,3,4,5,2
c. Prints: 1,2,3,4,5,3
d. Prints: 1,2,3,4,5,4
e. Prints: 1,2,3,4,5,5
f. Runtime error
g. Compiler error
h. None of the above


c Prints: 1,2,3,4,5,3
Java evaluates expressions from left to right while respecting operator precedence.
j = 1 + (((2 * 3) % 4) + 5)

I tried to reduce this:

Clearly I'm lost. Can someone do the simplification/reduction so I can see how operator evaluation/precedence works?

comp.lang.javascript FAQ:
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Your calculation is right...
only thing is the final print statement is
i,e 8 % 5 = 3
Hope this helps
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
Yeah, the most obvious thing I missed. Looks like I'm okay w/operator precedence.
I guess I need a "how to pay close attention to detail" tutorial.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Allow me to make a correction to the parenthesis. The uncorrected version that you quoted is as follows.
j = 1 + (((2 * 3) % 4) + 5)

The corrected version is a follows.
j = (1 + ((2 * 3) % 4)) + 5
I will upload the correction soon.
Java evaluates expressions from the left to right while respecting operator precedence. The first operator is the addition operator on the left. Java first evaluates the left operand and then evaluates the right. However, the right operand is an expression that contains two operators with higher precedence--the multiplication operator and the remainder operator. Once the multiplication and remainder expressions are evaluated then the result becomes the right hand operand of the first addition operator. The result of the first addition operator becomes the left hand operand of the second addition operator.
The operator precedence rules necessary to answer this question are really just the basic rules of evaluating the multiplication and division operations before addition. The confusing concept to learn is really the fact that java evaluates the operands from left to right. Although a person would jump straight to the inner most parenthesis, Java does not make that jump. Instead, Java parses the expression from the left and pushes intermediate results onto the operand stack until the results are needed. The difference between the human approach and the Java approach is not an issue until side effects are introduced with the increment and decrement operators. Java will always perform those increment and decrement operations from left to right as each operand is parsed as Java moves through the expression from left to right. A person that tries to jump straight to the inner most parenthesis would try to start the incrementation inside the inner most parenthesis and then work outwards.
Both people and Java respect parenthesis, but we parse expressions differently. Java does not want to waste time by scanning an entire expression to find the inner most parenthesis. Instead, Java makes use of the operand stack to save intermediate results until they are needed. In contrast, people don't have a reliable operand stack so we just jump to the inner most parenthesis and work outward.
I have included this question on the exam because I think that it is very educational and because it does NOT require memorization of the operator precedence chart beyond what we all learned in elementary school when we learned arithmetic. Multiplication and division operations are evaluated before addition and subtraction. However, this question is beyond the scope of the real exam. For the purposes of the real exam you really only need to know that the cast operator has higher precedence than addition, subtraction, multiplication and division. Of course, you also need to know that the unary operators have the highest precedence.
On the real exam, about the only operator precedence question that you will find will be a trick question where the cast operator appears to be casting the result of an entire expression to some narrow type such as byte, short or int. In reality, the cast operator is casting only the first operand to the more narrow type. The resulting type of the entire expression will still be some wider type such as float or double.
[ February 03, 2003: Message edited by: Dan Chisholm ]

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
I nearly forgot to mention that the real exam does contain questions involving the conditional "and", &&, and the conditional "or", ||, operators. Make sure that you understand both of the those operators.
It is sorta covered in the JavaRanch Style Guide.
subject: Operator Precedence
It's not a secret anymore!