In mathematics, an operation is an action where one or more operands with the help of an operator leads to a solution. The operator can be one of the counters and operands can be represented by, for example, a number.

When solving technical problems, it is common to have to do several numerical calculations. Initiating a calculation in Java is very easy, and Java also provides the standard class *java.lang.Math* contains several common mathematical calculations — for example, the square root and trigonometric functions.

Initiating a calculation in Java is easy as the most common calculation methods are available by default in Java.

Below are some of the most common math operations in Java that you should be familiar with.

Calculation | Notation | Data type resultat | Ex. int A = 10, int B = 4 |
---|---|---|---|

Addition | + | int | A + B = 14.0 |

Subtraction | – | int | A – B = 6 |

Multiplikation | * | int | A * B = 40 |

Division | / | double | A / B = 2.5 |

Modulus | % | double | A % B = 2.0 |

Less than | < | boolean | A < B = false |

Greater than | > | boolean | A > B = true |

Less than or equal to | <= | boolean | A <= B = false |

Greater than or equal to | >= | boolean | A >= B = true |

Equal to | == | boolean | A == B = false |

Not equal to | != | boolean | A != B = true |

Let’s have a look at some math calculations in Java. We recognize the mathematical operations, and they are hopefully nothing new. However, it is important to be careful what data type you declare to a variable. Consider the following code:

public class mathematical_calculations { public static void main(String[] args) { // Variables of type double double A = 10; double B = 4; System.out.println(A+B); System.out.println(A-B); System.out.println(A*B); System.out.println(A/B); } }

Provides the result

14.0 6.0 40.0 2.5

Which is the result we expect from each mathematical operation. However, if A, and B instead are integers, we get the following results:

public class matematiska_operationer { public static void main(String[] args) { // Variables of data type Integer int A = 10; int B = 4; System.out.println(A+B); System.out.println(A-B); System.out.println(A*B); System.out.println(A/B); } }

Provides the result

14 6 40 2

That is, a mathematical operation with a particular data type will result in a result of the same data type. For example, a division with 10/4 (which normally becomes 2.5), instead becomes 2, since a division of two integers will result in an integer type result – the decimal sign will, therefore, left out.

Absolutely! If we look at the same example again, but with different data types:

public class matematiska_operationer { public static void main(String[] args) { // Variables of different data types int A = 10; double B = 4; System.out.println(A+B); System.out.println(A-B); System.out.println(A*B); System.out.println(A/B); } }

The results are

14.0 6.0 40.0 2.5

Which is the same result we got in the case when both variables were of the data type double. Furthermore, Java has the priority order that a mathematical operation that contains a floating-point will result in a floating-point. Since we performed mathematical operations with both integers and floating-point (double), Java will prioritize the floating-point, so the result is a double.

The operation that is usually a little tricky to understand at first is Modulo, it returns the remainder a division. Let’s take an example to show Modulus calculations. Say you have a cinnamon bun that is 21 cm long (yes, it’s a long cinnamon bun). You want to divide it into precisely 6 cm long pieces (critical that they are exactly 6 cm). You can then split the cinnamon bun length into three pieces, and we then have 3 cm left of the cake, the 3 cm is our leftover.

Additionally, below are some more examples of calculations

int x = 7; int y = 2; int mod = x % y; // mod then becomes the rest when we divide 7 by 2 -> mod = 1 int x = 8; int Y = 4; int mod = x % y; // Here we get mod = 0, because we get no remainder when we divide 8 by 4 int x = 9; int y = 5; int mod = x % y; // The remainder becomes -> mod = 4

We use the so called Equality and Relational operations to determine if a value is greater, smaller or equal to another value. Let’s take the same example again:

public class mathematical_operations { public static void main(String[] args) { // Variables of different data types int A = 10; int B = 4; System.out.println(A > B); // Is A greater than B? System.out.println(A < B); // Is A smaller than B? System.out.println(A <= B); // Is A smaller or equal to B? System.out.println(A >= B); // Is A bigger or equal to B? System.out.println(A == B); // Is A exactly the same value as B? } }

The result in this case are

true false false true false

That is, if the operation is true, the result is true, and if the operation is false, the result is false. The data type that results is therefore of the boolean type. Moreover, we will have a look at the conditional operators a few pages ahead.