wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator precedence and associativity confusion........ 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 "Operator precedence and associativity confusion........" Watch "Operator precedence and associativity confusion........" New topic
Author

Operator precedence and associativity confusion........

Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
Hi all,

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[0] && ( b[1] | b[2] ) )
which actually gives me wrong answer, according to the buk....it has been evaluated as if( (b[0] && b[1] ) | b[2] ) 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

anyone.......

thanx
amit
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
if ( b[0] && b[1] | b[2] //How is this evaluated???)[


Hi Amit

logical OR(|) has higher precedence than conditional AND(&&).....right???

yes u r correct

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[0] && b[1] ) | b[2] )



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



whenever JVM see the first + operator it assumes ordinary unary plus,but if it see another + near by it assumes as increment operator.

so the code becomes

i++ + j++ + ++z

After that it will take it as unary plus(suppose if there is one more plus sign between j and i it throws error)

hope now U R clear
Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123
logical OR(|) has higher precedence than conditional AND(&&).....right???

yes u r correct

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[0] && b[1] ) | b[2] )


Hi Vidya ,
From your point of view if we are evaluating

a+ b * c
then JVM should interpret it as (a + b ) * c as * has higher precedence and + .
But it doesn't happen like this .


SCWCD 1.4, SCJP 1.4
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Originally posted by vidya sagar:


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[0] && b[1] ) | b[2] )


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

a4=1, b4=0, c4=0, false, Case 4: false && true | true
a5=1, b5=0, c5=0, false, Case 5: false && (true | true)
a6=1, b6=0, c6=1, true, Case 6: (false && true) | true

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%)
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Hi joe

In the case 1 why b value is not printing one,++ operatore has higher precedence than || (to my knowledge)

can u explain it???
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operator precedence and associativity confusion........
 
Similar Threads
Short Circuit operators and Bitwise operators
plz Explain me line by line this code
Non static variable is incrementing for method call
Operators - Mughal p.56 Ques 3.7
Question from K&B Book