Java Operator Precedence.

When two mathematical operators have the same precedence, they are performed in a left-to-right order.
  • Multiplication (*) and division (/) both have equal Operator Precedence but higher than the plus (+) and minus (-).
  • Plus (+) and minus (-) both have equal Operator Precedence.
  • All with the highest Operator Precedence is performed first and then will operators be performed after a left to right order.
Operator precedence example:
...
x = 5 + 3 + 8 * 9 + 6 * 4;
// is evaluated multiplication first, left to right. Thus,
// 8*9 = 72, and 6*4 = 24. Now the expression is essentially.
// x = 5 + 3 + 72 + 24; and result will be x=104;
...
To override this you can use parentheses and/or nesting parentheses:
...
TotalPersonSeconds = ( ( (NumMinutesToThink + NumMinutesToType) * 60) *
(PeopleInTheOffice + PeopleOnVacation) );
...
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.