Java Arithmetic Operators Tutorial

In this section, we will learn what arithmetic operators are and how they work in Java.

Note: we’re assuming you already read the Java operators section.

What is Arithmetic Operator in Java?

Those symbols that we used to do basic mathematical operations are called arithmetic operators and the table below shows these operators:

Arithmetic operators list in Java

In the table below, you can see the list of arithmetic operators.

Operator

Symbol

Addition Operator

+

Subtraction Operator

Multiplication Operator

*

Division Operator

/

Modulus Operator

%

Increment Operator

++

Decrement Operator

Java Addition `+` Operator:

We use this operator to add two values (operands) together. These values can be a variable, constant values (like 1.22, 2, 3,4 etc.) or the result of expression or the returned value from a method call.

Addition `+` Operator Syntax

LeftOperand + RightOperand;

Addition `+` Operator Example:

public class Simple {
    public static void main(String[] args){
        int integer = 10;
        int result = 2 + integer;
        System.out.println(result);
    }
}

Output: 12;

Java Subtraction `-` Operator:

This operator is on the contrary to the addition operator and is used to subtract two operands (values) from each other. Just like addition operator, the operands can be either a constant value like (10, 4 ,2, 43.22 etc.), the result of calling a method or the value of a variable.

Subtraction `-` Operator Syntax:

LeftOperand - RightOperand;

Subtraction `-` Operator Example:

public class Simple {
    public static void main(String[] args){
        int integer = 10;
        int result = 2 - integer;
        System.out.println(result);
    }
}

Output:

-8

Java Multiplication `*` operator:

When we want to multiply two values into each other, we use multiplication `*` operator. The values that can be used as the operands of the multiplication-operator can be the value assigned to a variable, constant values or the result of a method call.

Multiplication `*` operator Syntax:

LeftOperand * RightOperand;

Multiplication `*` operator Example:

public class Simple {
    public static void main(String[] args){
        int integer = 20;
        int result = integer * integer;
        System.out.println(result);
    }
}

Output: 400

Java Division `/` operator:

On the contrary, to the multiplication operator, this operator is used to divide the operand on the left side by the operand on the right side of the division `/` operator. Just like multiplication operator, the values that are applicable to this operator are constant values, variables and the returned value from calling a method and also the result of an expression.

Division `/` operator Syntax:

LeftOperand / RightOperand;

Division `/` operator Example:

public class Simple {
    public static void main(String[] args){
        int vOne = 100;
        int vTwo = 200;
        int result = vTwo / vOne;
        System.out.println(result);
    }
}

Output: 2

Java Modulus `%` Operator:

Let me ask a question: how many 3 are in 10?

If you look carefully, it is 3. This is because if we multiply 3 to 4 we will get 12 which are two values over the number 10 but if we multiply 3 by 3 the result is 9. Now if we subtract the value 10 from 9 what’s the remainder? It is 1.

We use modulus operator to get that remainder.

Modulus `%` Operator Syntax:

LeftOperand % RightOperand;

Modulus `%` Operator Example:

public class Simple {
    public static void main(String[] args){
        int result = 10 % 3;
        System.out.println(result);
    }
}

Output:

1

Java Increment `++` Operator:

This operator is used to increase the value assigned to a variable by one.

This operator can be set on the left or right side of the target variable.

Increment `++` Operator Syntax:

++variable;

variable++;

Increment `++` Operator Example:

public class Simple {
    public static void main(String[] args){
        int valueRight = 13;
        int valueLeft = 13;
        int left = ++valueLeft;
        int right = valueRight++;

        System.out.println("left-variable: "+ left+", right-variable: "+ right+", valueRight: "+ valueRight);
    }
}

Output:

left-variable: 14 , right-variable: 13, valueRight: 14

Here’s the difference:

If the `++` operator appeared on the left side of a variable like ` int left = ++valueLeft;` that means first increase the value assigned to the operand (in this case `valueLeft`) and then proceed to the rest of operations. So the assigned value to the `left` variable will be 14.

On the other hand, if the `++` operator appeared on the right side of a variable like ` int right = valueRight++;`, the value assigned to the operand (in this case `valueRight`) will be increased by one, but this is only guaranteed for the next time we call the target variable. So the final value that is assigned to the variable `right` in this example is 13 because the value of the `valueRight` did not change right away and the `right` variable took the old value stored in this variable.

Java Decrement `–` Operator:

This operator is used to decrease the value assigned to a variable by one.

Notes:

  • This operator can be set on the left and right side of the target variable.
  • Rules that applied to `++` operator also apply to `–` operator as well.

Decrement `–` Operator Syntax:

--variable;

variable--;

Decrement `–` Operator Example:

public class Simple {
    public static void main(String[] args){
        int valueRight = 13;
        int valueLeft = 13;
        int left = --valueLeft;
        int right = valueRight-- + valueRight;
        
        System.out.println("left-variable: "+ left+", right-variable: "+ right+", valueRight: "+ valueRight);
    }
}

Output:

left-variable: 12, right-variable: 25, valueRight: 12

Note: The basic arithmetic operations mentioned in this section apply to operands from left to right.

For example, if we have `int res = 10/2; ` the value 10 is divided by 2 not vice versa!

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies