Last Updated on

*Time needed: 10 minutes*

This tutorial will show how to perform simple arithmetic operations in Java. So far in the previous tutorials we never did any operations on our data, except the string concatenation, such as the one shown below.

String concatenated = "abc" + "def";

The *concatenated *variable will hold an "abcdef" value.

Arithmetic operators in Java are almost the same as these known from the math lessons, with one caveat: there are no *types *in mathematics. Variables can hold only a subset of total numbers: the size and precision are the two limiting factors.

## Assignment and comparison

Assignment operator is perhaps the most widely used one. We've used it in almost every example in the previous tutorials. It's represented by a single equals sign. It assigns the right value to the left operand. Here we assign 30 to an "age" variable.

int age = 30;

The comparison operator looks similar, because it consists of a double equals sign, but it does a completely different thing. It compares two values and returns a *true *or *false*.

int age = 30; int weight = 55; System.out.println(age == weight); // Will print "false"

It is very important not to mistake one for another, because it can lead to some hard to find bugs. Take a look at the following code. It will print the result of an assignment, rather than the result of comparison.

int age = 30; int weight = 55; System.out.println(age = weight); // Will print "55"

## Addition, subtraction, multiplication

The operations are very straightforward, I'll just leave a small code excerpt which shows how to use them.

System.out.println(1 + 2); // Addition, will print 3 System.out.println(10 - 5); // Subtraction, will print 5 System.out.println(3 * 5); // Multiplication, will print 15

There's just one risk. We can exceed the value our type can store. When the type is not explicitly provided, such as in the examples above, it will be interpreted as an integer.

We can check the maximal and minimal number an integer can hold with the following code.

System.out.println(Integer.MAX_VALUE); // Will print 2147483647 System.out.println(Integer.MIN_VALUE); // Will print -2147483649

If we ever exceed that number, a strange thing will happen. We will get a negative number, as shown below.

int maxInteger = Integer.MAX_VALUE; System.out.println(maxInteger + 1); // Will print -2147483648

This is called an *integer overflow*, and happens when the result can no longer be represented by our type, because the value is outside of its range. What we're receiving is the minimal possible number that an integer can hold. You can interpret it this way: when we exceed the maximum number, we begin to count from the lowest possible number, which is -2147483648 for an integer. The same can happen for multiplication and subtraction.

In some of the cases, and IDE can warn us about it, but I would strongly advise against relying on it, because it works only for the most basic cases, when the values are known in compile time.

int minInteger = Integer.MIN_VALUE; System.out.println(minInteger - 1); // Will print 2147483647 System.out.println(123456 * 123456); // Will print -1938485248

## Division

Division can be also tricky, because integer numbers cannot represent fractional parts.

System.out.println(3 / 2); // 1.5, but will print 1 System.out.println(10 / 3); // 3.3(3), but will print 3 System.out.println(15 / 4); // 3.75, but will print 3

Notice that these numbers are not rounded. The decimal part is simply discarded, so both the 3.3(3) and 3.75 will simply turn into 3. In the next tutorial we'll see how to use different data types to represent such values.