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:
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:
Multiplication(*): In order to multiply values/variables, this arithmetic operator in Java is practiced. You may follow the syntax provided below to perform multiplication
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:
Modulus(%): A modulus operator in Java returns the remainder of two values/variables after division. The syntax written below is practiced using this operator:
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.
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:
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:
Multiplication(*): The Java code written below multiplies two variables and returns the result.
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.
Division(/): To practice this, the following Java code is practiced using the division operator.
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:
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.
The output of the above code is provided below:
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.
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:
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.