next up previous
Next: Numeric overflow Up: Unit 04 Previous: Writing of numbers of

Integer expressions

The following fragment of a Java program shows expressions with operators on integers:

int a, b, c;
a = 10 / 3 + 10 % 3;
b = 2 * -3 + 4;
c = 2 * (a + b);

The following precedence rules between operators hold in Java (these are the same as those used in arithmetics):

  1. unary +, unary - (e.g., -x)
  2. *, /, %
  3. +, -

Brackets can be used to change the way subexpressions have to be grouped. Note that in Java, only round brackets, ( and ), and not square or curly brackets, can be used to group subexpressions

Example: In Java, the expression a+b*-c is equivalent to a+(b*(-c))


next up previous
Next: Numeric overflow Up: Unit 04 Previous: Writing of numbers of