• 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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


post increment(++) or pre increment operators have highest precedence..

for eg:
int x = 4;
int y = x * 2 + ++x;

now according to the precedence x should increment to 5 first and then x is reassigned so the answer would be y = 5 * 2 + 5(y=15)..

But the right solution is y = 4* 2 + 5(y=13)..


Why and How??

Thanks in advance...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You are confusing precedence with evaluation order. Just because you (and actually, the majority of people) instinctively evaluate based on precedence, that doesn't mean the computer does that. The JLS specifies how expressions are evaluated, and for the most part, it is done left to right.

Henry
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:The JLS specifies how expressions are evaluated, and for the most part, it is done left to right.Henry


Of course you are correct, Henry, but let's go further. In terms of evaluation order and precedence, how would you explain that this code outputs "7 7," rather than "9 7?"
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is evaluated from left to right. You take 1 and add something to it. Then you find that the operator to the right of the two has a higher precedence, so you add the result of that expression evaluated from left to right to 1 and Bob's your uncle: 7.
There is no inconsistency between precedences and left‑to‑right.

Kiruthika Dhanajeyan wrote:post increment(++) or pre increment operators have highest precedence..

Well, actually something like () behaves as if it had a higher precedence than the increment operators. The postfix operators have a higher precedence than the prefix operators, and prefix operators higher than multiplicative (* / %).
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

  • evaluates from left to right
  • x *
    4 *
  • Now checks is there any higher precedence operator than * then answer is yes i.e ++ so we get

  • 4 * 2 +   ( ++4 ) // ++ is evaluated first but x * 2 where value of x is already stored i.e 4

    4 * 2  +  ( 5 ) then

  • 4 * again checks is there any higher precedence operator than * then answer is no then

  • ( 4 * 2 ) + 5
    8 + 5

  • 8 + again checks is there any higher precedence operator than + then answer is no operator available then

  • 8 + 5  = 13 is the answer.

     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Same with
    Evaluates from left
  • 1 + checks any higher precedence operator than + so answer is yes i.e. * but we have in memory 1 + already so

  • 1 + kept as it is and * is evaluated so

  • 1 + ( 2 * 3 ) we get  1 + 6 then again checks any  higher precedence operator than + ? then answer is no, so

  • (1 + 6) executed y = 7

  • Same with  2 * 3 + 1


  • 2 *  any higher precedence operator to right side than *  ? answer is no then

  • ( 2 * 3) + 1 so we get 6 + 1 again check higher than +? answer is no then

    ( 6 + 1 ) = 7 is the answer.



     
    Kiruthika Dhanajeyan
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks all...especially patil....Crystal clear explanation..Thanks for your time......
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are welcome  
     
    What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic