Arithmetic Operator in Java

In this post, we will learn an arithmetic operator in Java. Arithmetic operators in Java are required to perform arithmetic operations like addition, subtraction, multiplication, division, and modulus. We will learn all the available arithmetic operators in Java with an example of each.

List of arithmetic operator in Java

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.

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

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

Frequently Asked Questions

What are arithmetic operators in Java?

Arithmetic operators in Java are symbols or keywords used to perform mathematical calculations, such as addition, subtraction, multiplication, and division.

How do you handle integer division in Java?

Integer division in Java truncates the decimal part. For example, 7 / 3 results in 2 because it discards the fractional part.

What is the modulus operator (%) used for in Java?

The modulus operator % returns the remainder of a division operation. For example, 7 % 3 equals 1 because 7 divided by 3 leaves a remainder of 1.

How can I perform arithmetic operations on floating-point numbers in Java?

To perform arithmetic with floating-point numbers in Java, use the appropriate data types (float or double) to maintain decimal precision.

What is operator precedence in arithmetic expressions in Java?

Operator precedence determines the order of evaluation in arithmetic expressions. For instance, multiplication and division have higher precedence than addition and subtraction.

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