Arithmetic Operations


This is a fairly easy tutorial. You will learn how to add, subtract, multiply, divide, and find remainders of numbers in Java. We will also explain about assignment operators and increments and decrements. Don't worry, all the terms and operations will be explained.

Arithmetic Operators

Let's us now go into the mathematical operations in Java. These are fairly simple operations that include addition, subtraction, multiplication, division, and remainders. Check the table below to understand how they work, but before assume that you have three integers which are x,y, and z.

border="1" style="text-align:center;">
Operation Operator Java Expression Description
Addition + z = x + y; Add x and y then store the sum in z
Subtraction - z = x - y; Subtract y from x then store the sum in z
Multiplication * z = x * y; Multiply x and y then store the result in z
Division / z = x / y; Divide x by y then store the value in z
Remainder % z = x % y; Get the remainder from dividing x by y then store it in z

Check how the Arithmatic Operation works by checking the code below.

public class Arithmetic
{
   public static void main(String args[])
   {
      int x = 9;
      int y = 2;
      int z = 0;

      z = x + y;
      System.out.println("x + y = "+z);

      z = x - y;
      System.out.println("x - y = "+z);

      z = x * y;
      System.out.println("x * y = "+z);

      z = x / y;
      System.out.println("x / y = "+z);

      z = x % y;
      System.out.println("x % y = "+z);

   }
}

Output:

x + y = 11 x - y = -7 x * y = 18 x / y = 4 x % y = 1

Note:Dividing an integer by an integer will result in an integer. Therefore, x / y will give 4 and not 4.5.

Increments and Decrements

Incrementing a variable or decrementing it is an operation that is commonly used in Java. These operators will be used in later tutorials and you will encounter them in almost any large program. Therefore, focus on understanding them.

Now lets begin with incrementing. To put it simply, incrementing is adding one to a variable. Consider the code below.

int x = 3;
x++;


The value of x now is 4. x++; is equivilant to x = x + 1; but we use x++ because it is faster to code and run.

Now we will move to decrementing. This is the opposite of incrementing which means that instead of adding one to the variable, we subtract one. Look at the code below.

int x = 6;
x--;


The value of x now is 5. Again, this is equivilant to x = x - 1;.

Check the code below for a preview of incrementing and decrementing.

public class IncDec
{
   public static void main(String args[])
   {
      int x = 5;
      int y = -2;

      System.out.println("x is : "+x);

      System.out.println("y is : "+y);

      x++;
      y--

      System.out.println("x now is : "+x);

      System.out.println("y now is : "+y);
   }
}

Output:

x is : 5 y is : -2 x now is : 6 y now is : -3

Operator Precedence

Consider the following line of code,

x = 2 + 1 * 5;

Some would think that the value in x now would be 15. However, the multiplication operator has precedence over the addition operator. This means that multiplication would be done first then addition. Therefore, the value in x would be equivilant to the following:

x = 2 + (1 x 5) = 2 + 5 = 7.

To avoid this mistake, we can use parantheses. Everything inside the paranthesis will be computed first, so to have x = 15 we have to do the following:

x = (2 + 1) * 5;

The list bellow shows the precedence of operators in Java.

  1. Parantheses
  2. Increment
  3. Decrement
  4. Multiplication
  5. Division
  6. Remainder
  7. Addition
  8. Subtraction

Assignment Operators

The final part in this tutorial is the Assignment Operators. The most common assignment operator is the equal "=" operator. However, there are more assignment operators which we will explain now. Check the code below:

x = 3;
x *= 3;


Well let's explain what this code does. x *= 3; is equivilant to x = x * 3. Instead of writing this whole statement, we use the assisgnment operator *= for faster coding and running. Check the table below for the assignment operators.

border="1">
Assignment Operation Equivilance
x += y x = x + y
x -= y x = x - y
x *= y x = x * y
x /= y x = x / y
x %= y x = x % y

Congratulations

You now know how to add variables and how operators work in Java. Move on to the next tutorial but first check the important notes below.

Important Notes

Assignment Operators Precedence

The assignment operators rank last with precedence so always keep that in mind while using them. You don't want any wrong output.