Java

Arithmetic Operators in Java | Explained

The operators are the building blocks of any programming language and so as in Java as well. Arithmetic operators perform few basic mathematical calculations on variables/values. The operations include addition, subtraction, division, multiplication, division, and finding the remainder. This article provides the working and usage of arithmetic operators in Java.

How arithmetic operators work in Java

This section provides a list of arithmetic operators supported by Java and the working of the operators is also described.

Addition(+): This operator shows the result of adding two or more two variables/values. The following syntax describes the addition operation:

a+b;

Subtraction(-): The subtraction of two or more than two operands is supported by the () operator in Java. The following syntax assist in using this operator:

a-b;

Multiplication(*): In order to multiply values/variables, this arithmetic operator in Java is practiced. You may follow the syntax provided below to perform multiplication

a*b;

Division(/): This operator allows you to multiply several values/variables in Java and returns the quotient obtained after division. To use this operator, you must follow the syntax provided below:

a/b;

Modulus(%): A modulus operator in Java returns the remainder of two values/variables after division. The syntax written below is practiced using this operator:

a%b;

How precedence works within arithmetic operators

The precedence is a crucial factor on which the working of operators depends. For precedence, the arithmetic operators may be divided into two categories named an additive and multiplicative additive. The multiplicative operators include *, / and % whereas the additive operator includes only +, and -.

If several arithmetic operators are used in a single expression, then the multiplicative operators have higher precedence than additive operators.

How to use arithmetic operators in Java

This section provides the usage of each arithmetic operator to perform the associated operations.

Adding two values: The Java code provided below shows the application of + operator on two integer variables.

package newpack;

public class ArithOp {

public static void main(String[]args) {
       
        //declaring variables
        int x=4, y=7, sum;
       
        //adding x and y
        sum=x+y;
       
        //printing the sum
        System.out.println(sum);
    }

}

The code is described as given below:

  • declares three variables
  • use the addition operator to add x and y. Then storing the answer in sum variable
  • prints the sum variable

The output of the code is mentioned below:

Subtraction(-): As discussed earlier, the subtraction operator shows the result after deducting one value from another. The following Java code shows the usage of subtraction operator:

package newpack;

public class ArithOp {

public static void main(String[]args) {
       
        //declaring variables
        int x=11, y=17, sub;
       
        //subtracts x from  y
        sub=y-x;
       
        //printing the answer
        System.out.println(sub);
    }

}

The above-stated code subtracts y from x and stores the answer in variable sub. Lastly, the value of sub is printed. The output can be observed in the following image:

Graphical user interface, application, Word Description automatically generated

Multiplication(*): The Java code written below multiplies two variables and returns the result.

package newpack;

public class ArithOp {

public static void main(String[]args) {
       
        //declaring variables
        int x=4, y=7, mul;
       
        //multiplies x and  y
        mul=x*y;
       
        //printing the answer
        System.out.println(mul);
    }

}

The above code declares two variables and multiplies these variables. Moreover, the result is stored in a variable named mul and is printed as well.

Graphical user interface, text, application, Word Description automatically generated

Division(/): To practice this, the following Java code is practiced using the division operator.

package newpack;

public class ArithOp {

public static void main(String[]args) {
       
        //declaring variables
        int x=4, y=7, div;
       
        //divides x and  y
        div=y/x;
       
        //printing the answer
        System.out.println(div);
    }

}

The above-stated code declares x and y then divide them and stores the answer in the div variable. Lastly, the div variable is printed as can be seen in the output shown below:

Graphical user interface, application, Word Description automatically generated

Modulus(%): A modulus operator is practiced getting the remainder of the two variables that are being divided. We have executed the following code to use modulus operator in Java.

package newpack;

public class ArithOp {

public static void main(String[]args) {
       
        //declaring variables
        int x=3, y=11, modu;
       
        //divides x and  y
        modu=y%x;
       
        //printing the answer
        System.out.println(modu);
    }

}

The output of the above code is provided below:

Graphical user interface, text, application, Word Description automatically generated

Using various arithmetic operators in a single expression

Up till now, we have discussed arithmetic operators individually. Here we will present a Java program that shows the application of various arithmetic operators in a single Java expression.

package newpack;

public class ArithOp {

public static void main(String[]args) {
       
        //declaring variables
        int a=3, b=11, c=7, examp1, examp2;
       
        //using +, - and /
        examp1= (b/a)+(c-a)+(b-c);
       
       
        //using *, +, -, and %
        examp2= (b%c)+(a*c)-(c%a);
       
        //printing the answers
        System.out.println(examp1);
        System.out.println(examp2);
    }

}

The code is explained below:

  • declares five variables
  • uses the +, – and / operators on a,b and c variables whereas the answer is stored in examp1
  • uses the *, +, -, and % operators on a,b, and c variables and the answer is stored in examp2
  • the examp1 and examp2 variables are printed

The output is shown in the image below:

Graphical user interface, text, application, Word Description automatically generated

Conclusion

Arithmetic operators assist in performing several mathematical operations like addition, subtraction, division, multiplication, etc. This article provides the working of several arithmetic operators supported by java. The arithmetic operators are further categorized into additive and multiplicative arithmetic operators. The precedence of multiplicative operators is greater than the precedence of additive operators. Various examples in this post demonstrate the usage of arithmetic operators individually and in a single expression as well.

About the author

Adnan Shabbir