Arithmetic and Expressions

Do arithmetic in Java with +, -, *, / and %, see why dividing two ints drops the remainder, and follow the order in which Java works out an expression.

Novice · Java · Updated

By

Arithmetic

All programming languages can do basic arithmetic.

Operator What it does Example Result
+ Addition int sum = 5 + 4; sum is 9
- Subtraction int difference = 5 - 4; difference is 1
* Multiplication int product = 5 * 4; product is 20
/ Division int quotient = 20 / 4; quotient is 5
/ Integer division drops the remainder int quotient = 5 / 4; quotient is 1: an int can't hold the .25, so only the whole number is kept
% Remainder int remainder = 6 % 4; remainder is 2
++ Increment: adds 1 int count = 10; then count++; or ++count; count is 11, as if you'd written count = count + 1;

Example

int shotsMade = 5;
int shotsMissed = 3;
int totalShots = shotsMade + shotsMissed;

System.out.println(totalShots);
System.out.println();

// add another shot made
System.out.println(shotsMade); // prints 5
shotsMade++;
System.out.println(shotsMade); // prints 6

Expressions

Every line of code we give a computer is a set of instructions. Each instruction is broken down into 1 or more expressions. The best way to think of this is to think that a computer can only do ONE thing at a time, but it does them very quickly. (This is not completely true, but it is a good way to learn about Expressions.)

If you have an equation that looks like this...

int answer = 12 + 3 * 2 - 14 / 2 + 10 + 53;

... your computer would not solve the equation in one step. It would break each step down into simple expressions. First, it would determine how to group the expressions (like you were taught in middle school algebra)

int answer = 12 + (3 * 2) - (14 / 2) + 10 + 53;

Then, it would solve each expression one step at a time.

Java works out an expression one step at a time, and each step uses one operator and its two operands. Looking at the example above, (3 * 2) is one step: 3 and 2 are the operands, and * is the operator. Java calculates it first, then uses the result in the next step, 12 + 6.

// 1) - first, multiply and divide left to right
int answer = 12 + (3 * 2) - (14 / 2) + 10 + 53;

// 2)
int answer = 12 + (6) - (14 / 2) + 10 + 53;

// 3) - then, add and subtract left to right
int answer = 12 + 6 - (7) + 10 + 53;

// 4)
int answer = 18 - 7 + 10 + 53;

// 5)
int answer = 11 + 10 + 53;

// 6)
int answer = 21 + 53;

// 7) - finally, there is only 1 expression left
//    - that is to store the value 74 into the variable "answer"
int answer = 74;

You can make your code more readable by keeping your expressions simple by creating intermediate variables.

For instance, in the following example, we perform all arithmetic in a single line.

// Bill scored 15 points from 3-point shots
// and 8 points from 2-point shots.
// How many shots did Bill make?
int totalShots = 15 / 3 + 8 / 2;

The numbers in the above example are not meaningful, making the code difficult to read and maintain. The code could be rewritten with intermediate variables to make the code more readable as follows.

// Bill scored 15 points from 3-point shots
// and 8 points from 2-point shots.
// How many shots did Bill make?
int pointsFromThree = 15;
int pointsFromTwo = 8;

int threePointShots = pointsFromThree / 3;
int twoPointShots = pointsFromTwo / 2;

int totalShots = threePointShots + twoPointShots;

By adding intermediate variables, our code becomes easier to maintain. The first example may seem better because it is a single line of code. However, fewer lines of code do not always equal better code. Code that has clear intent is easier to debug and maintain.

Do it: share the pizza slices fairly

A team orders pizzas from Build-It Pizzeria for a meeting. Everyone should get the same number of whole slices, and the slices nobody gets go back to the kitchen.

The program starts with three values:

  • pizzas: how many pizzas were ordered: 4.
  • slicesPerPizza: how many slices each pizza is cut into: 8.
  • people: how many people share them: 10.

Worked through:

Step Sum Answer
All the slices 4 × 8 32
Whole slices each person gets 32 ÷ 10, keeping only the whole part 3
Slices left over 32 − (3 × 10) 2

The program then prints exactly:

Total slices: 32
Slices each: 3
Slices left over: 2

Your job: replace the three 0s in the starter with the calculations.

  • totalSlices: the number of pizzas times the slices in each one.
  • slicesEach: how many whole slices each person gets, when totalSlices is shared out among people.
  • slicesLeftOver: the slices left after everyone has that many.
  • Calculate each one from the variables above it; don't type 32, 3 or 2 yourself.
  • Leave the first three variables and the main() method as they are.

The file holds the variables and a main() method with nothing around them: since Java 25, a small program can be written this way, and main() runs when the program starts.

Practiced live on the lesson page: write the Java right there and check it.

Cheat sheets