i was just trying to understand how operatoe precedence and associativity works by going through this link which says well about it....according to me.
So i tried a question from K&B buk Chap 3 page 200(ques no. 12)
what is the result? A. count = 0 B. count = 2 C. count = 3 D. count = 4 E. count = 10 F. count = 11
well everything goes fine but lin 18 i find some problem to evalute the result og the expression.....need help... well after going through that link's content i see that logical OR(|) has higher precedence than conditional AND(&&).....right??? so i proceed like......if( b && ( b | b ) ) which actually gives me wrong answer, according to the buk....it has been evaluated as if( (b && b ) | b ) to get the given ans....
where does operator precedence comes to play here .....?? is it sth related to operator associativity???well can someone, with an illustration, bother to explain me this thing(associativity ....prcedence). I'll be really thankful to 'em......
one more doubt how is this evaluated by the compiler (i+++j+++++z) assuming everythings properly initilized say to 0 and all are int primitives
When JVM interprets this line by seeing logical OR(|) it assumes left side of the OR operator as one set and right side of the operator as another set so it evaluates in this fashion
( (b && b ) | b )
That's actually not correct.
If that were true, then the left side of | would be evaluated, and then the right side of | would be evaluated, since | always evaluates both sides. However, this program demonstrates that that is not what is happening (see Case 4):
The output is: a1=1, b1=0, c1=0, true, Case 1: true || true & false a2=1, b2=0, c2=0, true, Case 2: true || (true & false) a3=1, b3=0, c3=1, false, Case 3: (true || true) & false
The results of case 1 and case 2 are identical, and the results of case 4 and case 5 are identical. After a short-circuit operator, the remaining parts of the expression are not evaluated at all, as shown by the fact that the b and c variables in those cases were not incremented.
In case 4, there is an expression ++a4==9 && ++b4==1 | ++c4==1 in which neither ++b4==1 nor ++c4==1 gets evaluated, despite the fact that the | operator always evaluates both sides before returning a result. This shows that evaluation runs left-to-right until the && short-circuit operator determines that no more evaluation is necessary (because the left side of && was false). The rest of the expression is ignored, and false is returned.
SCJA 1.0 (98%), SCJP 1.4 (98%)
posted 14 years ago
In the case 1 why b value is not printing one,++ operatore has higher precedence than || (to my knowledge)
can u explain it???
posted 14 years ago
The reason is that the rules of precedence do not apply the same way when using short-circuit operators ( && and || ).
In an expression with a short-circuit OR (||), if the left-hand side of the operator evaluates to true, then the right-hand side does not get evaluated.
In an expression with a short-circuit AND (&&), if the left-hand side of the operator evaluates to false, then the right-hand side does not get evaluated.
If a part of the expression does not get evaluated, then any side-effects of that part of the expression will not occur. This includes incrementing variables. Because the right-hand side of the expression does not get evaluated, the ++ operation never occurs.