Arithmetic Operator in Java

Arithmetic operator in Java let us perform basic math operations like addition, subtraction, multiplication, and division. The Arithmetic Operator in Java is essential for calculations in our code. Let’s dive in!

List of arithmetic operator in Java

When using the Arithmetic Operator in Java, one can easily manipulate numerical data. Let’s see the table that shows the detail of arithmetic operations:

OperatorMeaningExampleDescription
+Additiona+bAdds a and b
Subtractiona-bSubtracts b from a
*Multiplicationa*bMultiplies a and b
/Divisiona/bDivides a by b
%Modulusa%bComputes the remainder of dividing a by b

Addition operator

The addition operator is denoted by + (plus) which is an arithmetic operator. It adds more than one number and returns the result. The following code adds variables a and b and assigns the result to variable c.

public class Addition {
	public static void main(String[] args) {
		int a = 10;
		int b = 5;
		int c;
		c = a + b;
		System.out.println("The sum of " + a + " and " + b + " is " + c);
	}
}

Output:

The sum of 10 and 5 is 15

Subtraction operator

The subtraction operator is denoted by – (minus) which is an arithmetic operator. It subtracts first number from the second number. The following code subtracts variable a from b and assigns subtracted result to variable c:

public class Subtraction {
	public static void main(String[] args) {
		int a = 10;
		int b = 5;
		int c;
		c = a - b;
		System.out.println("The subtract from " + a + " to " + b + " is " + c);
	}
}

Output:

The subtract from 10 to 5 is 5

Multiplication operator

The multiplication operator is denoted by * (asterisk) which is an arithmetic operator. This multiplies more than one number. The following code multiplies two variables a and b and assigns result to variable c.

public class Multiplication {
	public static void main(String[] args) {
		int a = 10;
		int b = 5;
		int c;
		c = a * b;
		System.out.println("The multiple of " + a + " and " + b + " is " + c);
	}
}

Output

The multiple of 10 to 5 is 5

Division operator

The division operator in Java is denoted by / (forward slash) which is an arithmetic operator. The following code divides variables a by b and assigns result to variable c.

Integer division

Truncates decimal results.

public class Division {
	public static void main(String[] args) {
		int a = 10;
		int b = 5;
		int c;
		c = a / b;
		System.out.println("The division of " + a + " by " + b + " is " + c);
	}
}

Output

The division of 10 by 5 is 2

Floating-point division

double preciseResult = 15.0 / 4; // Result: 3.75  

Modulo operator

The modulo operator in Java is denoted by % which is an arithmetic operator. The modulo operator produces the result as a reminder of the division of one number by another. The following code demonstrates the modulo operator dividing a variable a by b and assigns result to variable c.

public class Division {
	public static void main(String[] args) {
		int a = 10;
		int b = 5;
		int c;
		c = a / b;
		System.out.println("The reminder of division " + a + " by " + b + " is " + c);
	}
}

Output

The reminder of division 10 by 5 is 0

Operator Precedence

Arithmetic operators follow PEMDAS rules:

  1. Parentheses ()
  2. Multiplication *, Division /, Modulus %
  3. Addition +, Subtraction -

Example:

int result = 10 + 5 * 2; // 20 (5*2 evaluated first)  
int corrected = (10 + 5) * 2; // 30  

Conclusion

Arithmetic operators are the backbone of math in Java. By understanding these operators we can handle calculations confidently. Keeping our code simple and test edge cases!

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments