• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

operator precedence

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(b2 != b1 = !b2)

the precedence order is ! is having higher precedence.
so i evaluated like (b2 != (b1 = !b2)) finally got the result true.
but when i run the program it gave compile error.

another one:
if (b2 != b1 && !b2)

again ! is having higher precedence.so i evaluated like (b2 != (b1 && !b2)) finally result is

true!=false--->false.but when i run the program,it gave
true.I am confused.help please.....
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shiva Mohan:
(b2 != b1 = !b2)

the precedence order is ! is having higher precedence.
so i evaluated like (b2 != (b1 = !b2)) finally got the result true.
but when i run the program it gave compile error.

another one:
if (b2 != b1 && !b2)

again ! is having higher precedence.so i evaluated like (b2 != (b1 && !b2)) finally result is

true!=false--->false.but when i run the program,it gave
true.I am confused.help please.....



! does have higher precedence that assignment operators, but != has higher precedence that =.

So b2 != b1 = !b2 is equal to b2 != b1 = (!b2) is equal to (b2 != b1) = (!b2) which doesn't make sense.

In the second case b2 != b1 && !b2, != has higher precedence that &&, so
b2 != b1 && !b2 is equal to b2 != b1 && (!b2) is equal to (b2 != b1) && (!b2).
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for letting me know where i was going wrong.One more doubt,when i
work on the below program as you said,i have trouble again.

int j= ++i + i + i++ +i + i++; initailly value of i is i=0
=1+i+1+i+2 (first i evaluated prefis and two postfix i's and finally i value is 3)
=1+i+1+3+2(since unary + has right to left associativity,right side unary is getting higher precedence to access)
=1+3+1+3+2(i got the result 10.but the actaul result is 7.why?)
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though the ++ operator is listed as having a higher precedence than other operators, it doesn't mean that all the ++ operations are done first.

The expression is evaluated left to right and the value of i is changed by any ++ operator.

So in the expression ++i + i + i++ +i + i++,

++i has the value 1. after the increment i's value is 1.

i has the value 1

i++ has the value 1. after the increment i's value is 2.

i has the value 2

i++ has the value 2. after the increment i's value is 3.

Now we add the values.

1 + 1 + 1 + 2 + 2 = 7.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith, still confusing.please hang on.


The expression is evaluated left to right and the value of i is changed by any ++ operator.



How would we got the opinion that this expression is evaluated left to right since prefix ++ having right associativity,i am asking.


2.doubt.
b2 != b1 && !b2

(b2 != b1) && (!b2),after evaluating !b2
b2 != b1 && true,i thought b2 value is changed to true.so
true != false && true gets evaluated.But when i print b2 value ,it is still printing false.why the b2 value true is not working for
b2 != b1 ?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the Java Language Specification 15.7.

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.

15.7.1 Evaluate Left-Hand Operand First
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated. For example, if the left-hand operand contains an assignment to a variable and the right-hand operand contains a reference to that same variable, then the value produced by the reference will reflect the fact that the assignment occurred first.

Thus:

class Test {
public static void main(String[] args) {
int i = 2;
int j = (i=3) * i;
System.out.println(j);
}
}

prints:

9

It is not permitted for it to print 6 instead of 9.


If b1 = true and b2 = false,

then b2 != b1 && !b2 is true.

Note that in this case the value !b2 will not change the value of b2. !b2 is the value of b2 after a negation.

So b2 is still false.
 
reply
    Bookmark Topic Watch Topic
  • New Topic