Originally posted by Prash Gali:
Hello uzma,
Since the left most expression is evaluated first, a becomes true. Hence other assignments dont take place which would result in
a = true, b = false, c = false
ranchers correct me if i am wrong.
Originally posted by Ram Roop:
The code provided here should not give any compilation errors. It would compile and run just fine. The output would be:
2
3
1
The order of execution would be:
1. initilization block execution.
2. member variable assignment.
3. constructor call.
Hope this is helpful.
[ January 12, 2006: Message edited by: Ram Roop ]
Originally posted by Deepthi Rallabandi:
Hi,
In java always evaluation will be from left to right.
suppose take this expression
2 + 3 * 4 - 1 * 7
here you can think of the operands in expression will be grouped as follows
step 1: 2 + (3 * 4) - (1 * 7) according to precedence rules
step 2: ((2 + (3 * 4)) - (1 * 7)) accrding to associative rules
step 3: for any operator to be applied both operands must be first evaluated.
so as evaluation is from left to write first 2 + (3 * 4) needs to be evaluated.
2+(3*4)=> 2+12=14 then
14 - (1*7)=> 14 - 7 = 7
so final result is 7.
so in your code the grouping will be as follows
(a = true) || (b = true) && (c = true)
=> ((a=true) || ( (b=true) && (c=true) ))
as evaluation will be from left to write
(a=true) is evaluated first which results in true and "a" is assigned value true .
as || is short hand operator if left operand is true, right operand is not evaluated.
so output will be true, false , false as default values for b and c are false.
Doesn't this means that operands of && operator must evaluated before operands of || , as && have higher precedence?The evaluation order also respects any parentheses, and the precedence and associativity rules of operators.