Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Unary operator and precedence evaluation

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone help me out on this

Is it true that the unary prefix operator has Right to left evaluation ?

Going by the following code

int i=0, j = ++i + ++i * ++i;
System.out.print(j);

output is 7

I think it is being evalutated as j = (1) + (2) *(3) if unary prefix is evaluated from right to left should it not be j = (3) + (2) * (1) which will yield 5 !!!
any help
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is that associativity only controls the order between two operators of the same precedence applicable to the same operand.

For example "++~i" is evaluated as "++(~i)" and (float)(int)5.5 = 5.0f, but 8/4/2 = 1 and a[5][3] = (a[5])[3].

In your example, the controlling rule is that the operands of binary operators are evaluated left then right.
[ December 12, 2004: Message edited by: Mike Gershman ]
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic