# Java Integer Arithmetic: Expressions & Operators

## Integer arithmetic expressions in Java

In Java, integer arithmetic is performed using “integer data types”. In Java Data Types, we discussed some of these types—byte, short, int and long—and gave examples of “integer constants”. An integer constant (e.g. 23, 0, -245) is the simplest example of an integer *expression*. However, most of the time, we write integer expressions by combining constants and variables with the following *arithmetic operators*:

For example, suppose we have the following declaration:

**int a, b, c;**

then the following are all valid expressions:

a + 39

a + b - c * 2

b % 10 //the remainder when b is divided by 10

c + (a * 2 + b * 2) / 2

The operators +, - and * all give the expected results. However, / performs *integer division*; if there is any remainder, it is thrown away. We say integer division *truncates*. Thus 19 / 5 gives the value 3; the remainder 4 is discarded.

But what is the value of -19 / 5? The answer here is –3. The rule is that, in Java, integer division truncates *towards* zero. Since the exact value of –19 ÷ 5 is –3.8, truncating towards zero gives –3.

The % operator gives the *remainder* when one integer is divided by another; for example,

19 % 5 evaluates to 4;

j % 7 gives the remainder when j is divided by 7;

You can use it to test, for instance, if a number **j** is even or odd. If **j % 2** is 0 then **j** is even; if **j % 2** is 1, **j** is odd.

## Precedence of operators

Java evaluates an arithmetic expression based on the usual *precedence* of operators: multiplication and division are done *before* addition and subtraction. We say that multiplication and division have *higher precedence* than addition and subtraction. For example, the expression

5 + 3 * 4

is evaluated by *first* multiplying 3 by 4 (giving 12) and *then* adding 5 to 12, giving 17 as the value of the expression.

As usual, we can use brackets to force the evaluation of an expression in the order we want. For example,

(5 + 3) * 4

first adds 5 and 3 (giving 8), and then multiplies 8 by 4, giving 32.

When two operators which have the *same* precedence appear in an expression, they are evaluated *from left to right*, unless specified otherwise by brackets. For example,

24 / 4 * 2

is evaluated as

(24 / 4) * 2

(giving 12) and

12 - 7 + 3

is evaluated as

(12 - 7) + 3

giving 8. However,

24 / (4 * 2)

is evaluated with the multiplication done first, giving 3, and

12 - (7 + 3)

is evaluated with the addition done first, giving 2.

In Java, the remainder operator % has the same precedence as multiplication (*) and division (/).

In Floating Point Numbers and Expressions, we explain how to work with floating-point numbers. In Java double to int and Other Conversions, we show how to mix integers and floating-point numbers in the same expression.

**Exercise**: What is printed by the following program? Check your answer by typing the program to a file **Exercise.java**. Compile the program and run it.

public class Exercise {

public static void main(String[] args) {

int a = 15;

int b = 24;

System.out.printf("%d %d\n", b - a + 7, b - (a + 7));

System.out.printf("%d %d\n", b - a - 4, b - (a - 4));

System.out.printf("%d %d\n", b % a / 2, b % (a / 2));

System.out.printf("%d %d\n", b * a / 2, b * (a / 2));

System.out.printf("%d %d\n", b / 2 * a, b / (2 * a));

}

}

## References

Java Programming – A Beginner’s Course

Related programming references

Article: Java Programming Tutorials: Java Integer Arithmetic

## This post is part of the series: Introduction to Java Programming

This is a series which aims to discuss and teach Java programming to the complete beginner. Absolutely no programming background is assumed.

- Introduction to Java Programming - An Overview
- Java - Data, Variable and Algorithm Explained To A Beginner
- Java Example: Algorithm and Program For Area of Square
- Java Programming For Beginners - Test, Debug, Document, Maintain
- JDK Java Compiler: The Java Development Kit
- Java Programming For Beginners - How To Compile And Run Java Programs
- Data Types, Constants And Variables
- Java Programming For Beginners - Characters and printf
- Java Programming For Beginners - Part 9
- Java Programming For Beginners - Part 10
- Java Programming For Beginners - Part 11
- Java Programming For Beginners - Part 12
- Java Programming For Beginners - Part 13
- Java Programming For Beginners - Part 14
- Java Programming For Beginners - Integer Data Types
- Java Programming for Beginners - Part 16
- Java Integer Arithmetic For Beginners
- Java Programming For Beginners - Part 18
- Java Programming For Beginners - Part 19
- Java double to int and Other Conversions