In this tutorial, you will learn about Java arithmetic operators, the symbols used for each operation, and examples for addition, subtraction, multiplication, division, modulus, increment, and decrement operators.
Arithmetic Operators in Java
Java arithmetic operators are used to perform mathematical calculations on numeric values. They work with primitive numeric types such as byte, short, int, long, float, and double. The same operators are also commonly used inside expressions, assignments, loops, and calculations in Java programs.
The main arithmetic operations supported in Java are listed below.
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
- Increment (Pre-Increment, Post-Increment)
- Decrement (Pre-Decrement, Post-Decrement)
The official Java documentation groups these symbols under operators used in expressions. You may also refer to Oracle’s Java operator documentation for the formal operator summary: Java Operator Summary.
Java Arithmetic Operators Table
The following table shows the arithmetic operator, symbol, example, and description for each arithmetic operation in Java.
| Arithmetic Operation | Operator Symbol | Example | Description |
|---|---|---|---|
| Addition | + | a + b | Returns the sum of a and b. |
| Subtraction | - | a – b | Returns the difference when b is subtracted from a. |
| Multiplication | * | a * b | Returns the product of a and b. |
| Division | / | a / b | Returns the quotient when a is divided by b. |
| Modulus | % | a % b | Returns the remainder when a is divided by b. |
| Increment | ++ | a++ is Post-Increment.++a is Pre-Increment. | Increments the value of a by one. Post-Increment: the current value is used first, and then a is increased. Pre-Increment: a is increased first, and then the updated value is used. |
| Decrement | -- | a-- is Post-Decrement.--a is Pre-Decrement. | Decrements the value of a by one. Post-Decrement: the current value is used first, and then a is decreased. Pre-Decrement: a is decreased first, and then the updated value is used. |
Java arithmetic operator syntax examples
The following syntax block shows the common forms of arithmetic expressions in Java.
result = a + b; // addition
result = a - b; // subtraction
result = a * b; // multiplication
result = a / b; // division
result = a % b; // modulus or remainder
count++; // post-increment
++count; // pre-increment
count--; // post-decrement
--count; // pre-decrement
Java Arithmetic Operators Example with int Variables
Now, let us see the demonstration of arithmetic operators with an example Java program, ArithmeticOperatorsExample.java. The program uses two integer variables and prints the result of each arithmetic operation.
Main.java
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println("Addition : (a + b) = " + (a + b));
System.out.println("Subtraction : (a - b) = " + (a - b));
System.out.println("Multiplication : (a * b) = " + (a * b));
System.out.println("Division : (a / b) = " + (a / b));
System.out.println("Modulus : (a % b) = " + (a % b));
a = 10;
System.out.println("During Post-Increment : a = "+(a++));
System.out.println("After Post-Increment : a = " + a);
a = 10;
System.out.println("During Pre-Increment : a = " + (++a));
System.out.println("After Pre-Increment : a = " + a);
a = 10;
System.out.println("During Post-Decrement : a = "+(a--));
System.out.println("After Post-Decrement : a = " + a);
a = 10;
System.out.println("During Pre-Decrement : a = " + (--a));
System.out.println("After Pre-Decrement : a = " + a);
}
}
Output
Addition : (a + b) = 15
Subtraction : (a - b) = 5
Multiplication : (a * b) = 50
Division : (a / b) = 2
Modulus : (a % b) = 0
During Post-Increment : a = 10
After Post-Increment : a = 11
During Pre-Increment : a = 11
After Pre-Increment : a = 11
During Post-Decrement : a = 10
After Post-Decrement : a = 9
During Pre-Decrement : a = 9
After Pre-Decrement : a = 9
How the Java addition, subtraction, multiplication, division, and modulus results are produced
In the example, a is 10 and b is 5. So a + b returns 15, a - b returns 5, a * b returns 50, a / b returns 2, and a % b returns 0 because 10 is exactly divisible by 5.
For increment and decrement operations, the difference between prefix and postfix form becomes visible when the operator is used inside another expression. In a++, the old value is used first. In ++a, the value is increased first and then used.
Integer Division and Remainder in Java Arithmetic
Java division behaves differently depending on the numeric type. When both operands are integers, Java performs integer division and removes the fractional part. When at least one operand is a floating-point value, Java performs floating-point division.
public class Main {
public static void main(String[] args) {
System.out.println(7 / 2);
System.out.println(7.0 / 2);
System.out.println(7 % 2);
}
}
3
3.5
1
In the first expression, 7 / 2 returns 3 because both operands are integers. In the second expression, 7.0 / 2 returns 3.5 because one operand is a double. The expression 7 % 2 returns 1, which is the remainder after dividing 7 by 2.
Division by zero with Java arithmetic operators
With integer arithmetic, division by zero causes an ArithmeticException. With floating-point arithmetic, division by zero may produce special floating-point values such as Infinity or NaN.
public class Main {
public static void main(String[] args) {
System.out.println(10.0 / 0.0);
System.out.println(0.0 / 0.0);
}
}
Infinity
NaN
For integer division, validate the divisor before using the / or % operator when there is a possibility that the value can be zero.
Java Arithmetic Operator Precedence
Java follows operator precedence rules when evaluating arithmetic expressions. Multiplication, division, and modulus are evaluated before addition and subtraction. Parentheses can be used to make the intended order clear or to change the default order.
public class Main {
public static void main(String[] args) {
int result1 = 10 + 5 * 2;
int result2 = (10 + 5) * 2;
System.out.println(result1);
System.out.println(result2);
}
}
20
30
In 10 + 5 * 2, multiplication is evaluated first, so the result is 20. In (10 + 5) * 2, the expression inside parentheses is evaluated first, so the result is 30. This is the Java equivalent of the usual arithmetic order of operations.
Prefix and Postfix Increment and Decrement in Java
The increment and decrement operators are unary arithmetic operators because they work on one variable. They are often used in loops, counters, and index updates.
| Expression | Name | Meaning |
|---|---|---|
a++ | Post-increment | Use the current value of a, then increase a by 1. |
++a | Pre-increment | Increase a by 1, then use the updated value. |
a-- | Post-decrement | Use the current value of a, then decrease a by 1. |
--a | Pre-decrement | Decrease a by 1, then use the updated value. |
When the operator is used as a separate statement, a++; and ++a; both increase a by one. The difference matters when the expression value is used in assignment, method arguments, or another calculation.
Arithmetic Operator Tutorials in Java
The following tutorials provide detailed explanations and examples for each arithmetic operator in Java.
- Java Addition Operator
- Java Subtraction Operator
- Java Multiplication Operator
- Java Division Operator
- Java Modulus Operator
- Java Increment Operator
- Java Decrement Operator
Java Arithmetic Operators FAQs
What are the basic arithmetic operators in Java?
The basic arithmetic operators in Java are + for addition, - for subtraction, * for multiplication, / for division, and % for modulus or remainder. Java also provides ++ and -- for increment and decrement.
Does Java follow the usual order of arithmetic operations?
Yes. Java evaluates multiplication, division, and modulus before addition and subtraction. Parentheses are evaluated first and can be used to control the order of calculation.
What is the difference between / and % in Java?
The / operator returns the quotient of a division operation. The % operator returns the remainder. For example, 7 / 2 gives 3 with integer operands, while 7 % 2 gives 1.
Is == an arithmetic operator in Java?
No. == is an equality operator in Java, not an arithmetic operator. Java does not have a === operator. Arithmetic operators are used for numeric calculations, while equality operators are used for comparison.
What happens when integers are divided in Java?
When both operands are integers, Java performs integer division and removes the fractional part. For example, 7 / 2 gives 3, not 3.5. To get a decimal result, use a floating-point value such as 7.0 / 2.
Editorial QA Checklist for Java Arithmetic Operators
- Does the page clearly distinguish arithmetic operators from comparison operators such as
==? - Does the division explanation mention integer division and floating-point division separately?
- Does the modulus section use the word remainder correctly?
- Do the examples show the difference between prefix and postfix increment or decrement?
- Are all Java examples marked with
language-javaand all result blocks marked withoutput?
Summary of Java Arithmetic Operators
In this Java Tutorial, we learned about different arithmetic operators in Java. The operators +, -, *, /, and % perform common arithmetic calculations, while ++ and -- update a variable by one. We also discussed integer division, remainder calculation, operator precedence, and the difference between prefix and postfix forms.
TutorialKart.com