Win a copy of Modern frontends with htmx this week in the Spring 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
  • 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 and evaluation

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Take a look at this,

public class OperatorPrecedence
{
public static void main(String[] args)
{
int a=2;
int b=10;

System.out.println(a-=2*b>b?--b:a++);
}
}
returns the output as : -8

Given the above piece of code containing a combination of operators having different order of evaluation and precedence,I would like to know where does the java compiler start to read it from left->right or right->left and then later evaluate on each operator according to its precedence and the order of evaluation?

Kindly explain.

Regards,
Anand Kapadi


 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
may be you will run this class again
because o/p is -7

and the order is like that
(a-=((2*b>b)?--b:a++))
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anand,
The order of evaluation of oeprators for the example posted is as follows.
All assignments operators operate from righto left
hence for the experision: a-=2*b>b?b--:a++, expression on right hand side will be evaluated first. All relational (> and conditional(?) operators operate frm right to left hence (b--:a++) will beevalueated first. After this evaluation, the expression will be :
(2*b>b?(b--:a++)), hence the output will be 2*b>b 9:3.
Result of this expression will be 9.
Hence a-=9 will evaluate to -7.
Let me know if this helps.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhijit Sontakey:
...conditional(?) operators operate frm right to left hence (b--:a++) will beevalueated first...


Not quite.

The conditional ? : operator (ternary) "is syntactically right-associative (it groups right-to-left), so that a?b:c?d:e?f:g means the same as a?b c?d e?f:g))." But note that this is not saying that a?b:c means the same as a?(b:c).

In fact, only one of the last two operands will be evaluated. The "boolean value is then used to choose either the second or the third operand expression... The chosen operand expression is then evaluated..."

Ref: Java Language Specification, section 15.25.
[ April 01, 2006: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, some of the confusion might be coming from the compound assignment operator, -=. Some spaces would also help human eyes.

The following is not correct Java syntax for a compound operator, but looking at it this way should help you see what's happening...

a = a - ( 2*b>b? --b : a++ )

Now, (2*b>b) evaluates as (2*10 > 10), which is true.

So in evaluating the right side of the assignment, we just have:

a - (--b) = 2 - 9 = -7.
 
reply
    Bookmark Topic Watch Topic
  • New Topic