File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in jls - order of Evaluation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in jls - order of Evaluation" Watch "doubt in jls - order of Evaluation" New topic

doubt in jls - order of Evaluation

mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
15.7 Evaluation Order
The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.
15.7.3 Evaluation Respects Parentheses and Precedence
Java programming language implementations must respect the order of evaluation as indicated explicitly by parentheses and implicitly by operator precedence.
My question is that these seem a bit contradictory to me. If evaluation order is always from left to right, then how can it be affected by parentheses or operator precedence? I can understand that the actual grouping of expression depend on operator associativity, and may be changed by using parentheses.
for eg a*b*c is same as (a*b)*c and not a*(b*c). But this is different from order of evaluation. For eg.
x = f1()+f2()*f3(); results in f1() invoked before f2() and
f3(), which supports 15.7 above, that the order is always from left to right.
Any views?
Milind Mahajan
Ranch Hand

Joined: Oct 23, 2000
Posts: 77
Hi Mohit,
This is what I think :
The operands are always evaluated from left to right. But the operation is performed depending on parentheses and precedence of operators involved. For example, see the code below :

class SomeC {

public static void main(String args[]) {
static int a1() {
System.out.println("I am a1()");
return 1;

static int a2() {
System.out.println("I am a2()");
return 2;

static int a3() {
System.out.println("I am a3()");
return 3;
The output is :
I am a2()
I am a1()
I am a3()
Thus methods a2(), a1() and a3() are called from left to right since, they are the operands for operators + and *. But see the result. It is 5. Meaning that the * operation took place first ( It's precedence is higher than + ) and then + operation took place.
Hope this helps.
Tell me what do you think.

mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
My question is that if the order of evaluation is fixed, then how can it respect parentheses and precedence as suggested in
15.7.3 above?
Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
I guess the following program shall better illustrate the concept.
int a=40,b=30,c=20;
if((a==b) && (++a>=c))
This prints 40.
If it goes by operator precedence first, it has to increment "a" first, coz', that's the highest priority(unary operator)! Moreover, between == and >=, the latter has a higher precedence. So, ++a>=c should be evaluated first... But this is not the case..
It first calculates the values of the operands that it has to use in the expression from "left to right".
So, it has now gotta do
int a=40,b=30,c=20;
if(40==30 && 41>=20) //it has not yet touched any of the values of any of the operands, they are just read from the variables.
Now, it uses operator precedence and parenthesis to evaluate this case. Since && doesn't allow the second operation to proceed if the first operation is true, a is not incremented at all. Hence the result 40. So, for evaluation of "operands value" alone, it parses from left to right, for evaluation of expression, it uses operator precedence and parentheses.
Hope I am clear.
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
How exactly is the term 'Evaluation' defined. is it finding the values of different subexpressions contained within the expression, or does it include applying the operators and getting the resulting value/variable that the expression denotes.
I agree. Here's the link:
subject: doubt in jls - order of Evaluation
It's not a secret anymore!