Java Tutorial - Basic Arithmetic Operators

The most basic way we use a programming language is to develop programs that performs mathematical calculations. The foundation of the most basic of all mathematical operations are arithmetic. Which in most programming languages covers addition, subtraction, division, multiplication, and modulus. Most other mathematical calculations can be done through the use of these basic operations. Below is a tutorial on how to perform arithmetic operations in Java.

Java Tutorial -

The basic arithmetic operations supported by Java as of the time of this writing are the following:
  • Addition (+) - is the operation of adding operands to get their sum, the symbol plus (+) is used to denote this
  • Subtraction (-) - is the operation for subtracting the value of one operant from the other, the symbol minus (-) is used to denote this
  • Multiplication (*) - is the operation for getting the product of the operands and is denoted by the symbol asterisks *
  • Division (/) - is the operation that divides a number with another number and is denoted by the symbol /
  • Modulus (%) - us the operation that gets the integral remainder when one number is divided by another, it is denoted by the symbol percent (%)

Java Addition Operator

The symbol plus (+) is used as the symbol for addition. This operator when used, means to add the number from it's left and right. For example:

System.out.println(1 + 2);

The symbol + is between the numbers 1 and 2. Hence the statement above means to add the number from the left and right of the symbol plus, which gives us the answer 3. Below is the output of the above code:

3

The above code shows example on how to add two numerical literals. We can add variables too:
int a = 2;
int b = 3;
System.out.println(a + b);
The above example uses two variables a and b with values 2 and 3. The plus sign is then applied on both numbers, by putting it in their middle, giving us the below answer:
5
The last example below mixes adding literal and variable numbers:
int a = 5;
System.out.println(a + 8);
So we have a variable a with value 5 and we use the plus symbol to add variable a with the number 8. The program will output the answer below:
13

Java Subtraction Operator

The minus (-) symbol is what we typically use to denote addition in Java. This symbol is typically shown between values of two numbers, and it means to subtract the value of the right from the left. Below is a simple example to explain how it works:

System.out.println(5 - 2);

The number to the right of the minus symbol is 2 and the number to the left is 5. It means subtract the value from the right which is 2, from the left which is 5, that will result to the value 3. Hence the output of the code is shown below:

3

The first example shows how to do subtraction from two literal values. Below is an example on how to do this on variables:
int a = 10;
int b = 5;
System.out.println(a - b);
And similar to discussed above, we subtract the value of variable b which is 5 from the variable a on the left which has the value of 10. The result is shown below:
5
And last example is subtraction between literals and variables, which is also allowed in Java.
int a = 21;
System.out.println(a - 8);
So again, we subtract the value from the right of the minus sign which is 8, from the value from the left which is the variable a with value 21. The result is shown below:
13

Java Multiplication Operator

Multiplication is a more processing intensive operation that addition and subtraction. But this is necessary for many of our programs, so that we don't need to do loop in some situations. This asterisk symbol (*) is used to denote a multiplication operation, which means to multiply the value to the left and right of the symbol For example:

System.out.println(2 * 3);

Here Java sees the asterisk and the left side has the value 2 and the right side is 3. The two are multiplied generating the result shown below:

6

The code we shown above again, shows two literals in action. We show the counterpart in variables:
int a = 5;
int b = 3;
System.out.println(a * b);
So again, the value to the left and right of the symbol asterisk is multiplied. Which are the values of the variables a and b, which are 5 and 3. The result is shown here:
15
Lastly, we mix again variables and literals to show that we can do it in Java:
int a = 4;
System.out.println(a * 8);
So the symbol asterisk has a to the left and literal 8 to the right. So we multiply the value of a which is 4 to the literal 8, that will result to:
32

Java Division Operator

Unlike multiplication, the position of the operant in division is very important. The symbol used for division is /, and the left value is the one we will divide by the value to the right. Example:

System.out.println(8 / 2;

So the above code wants to divide 8 by 2, because 8 is to the left and 2 is to the right. Hence the result is below:

4

We try an example with variables:
int a = 10;
int b = 5;
System.out.println(a / b);
So similarly, we want to divide a because it is on the left side, with the value of b which is on the right side. So we divide 10 by 5, that will result to:
2
Lastly is we combine variables and literals:
int a = 21;
System.out.println(a / 7);
So the value of a which is 21, will be divided by the literal 7. So we are dividing 21 by 7 in this example, which gives us:
3

Java Modulus Operator

Modulus is related to division. But instead on getting the answer, for example 21 / 7 is 3, what we want in modulus is to get the integral remainder after the division operation. For example, the remainder when 21 is divided by 7 is 0. When 22 is divided by 7, the remainder is 1. When 23 is divided by 7, the remainder is 2. And so on. Below is an example:
System.out.println(5 % 2);

So similarly, it is like dividing the left side which is 5, by the right side which is 2. So when we divide 5 by 2, the remainder is 1. Which is exactly the result of the code as shown below:

1

Applying it to variables is given below:
int a = 11;
int b = 5;
System.out.println(a % b);
So it is like dividing a with b, which is dividing 11 by 5. The operation will leave a remainder of 1, which is shown below:
1
And lastly, we show mix of variable and literal example:
int a = 18;
System.out.println(a % 5);
We are dividing a with 5, which is dividing 18 with 5. The remainder is 3.
3

Introduction to Java Topics