Arithmetic Operators

Purpose:

To do arithmetic operations in Java code.

Mechanics:

<expression1> <operator> <expression2>

Example:

private int myNumber = 5 + 2;

Usage:

  • <expression1> and <expression2> are represented by constants, variables, or any combination of parenthesized subexpressions.
  • <operator> is any arithmetic expression.

Restrictions:

The Three High-Priority Operators:

* Multiplication written as <expression1>*<expression2>, computes the product of the two expressions.
/ Division written as <expression1> div <expression2>, computes the integer quotient of <expression1> divided by <expression2>, ignoring the remainder. For example: 6 / 4 = 1 and -11 / 3 = -3.
% Modulo written as <expression1> mod <expression2>, computes the integer remainder of <expression1> divided by <expression2>. For example, 6 % 4 = 2 and -11 % 3 = -2.

The Two Low-Priority Operators:

+ Addition written as <expression1> + <expression2>, adds the two expressions.
- Subtraction written as <expression1> - <expression2>, substracts the second expression from the first.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu