Java Arithmetic Operators — Notes
What is a Java Operator?
A special symbol that performs operations on variables and values, used to manipulate data in expressions.
Arithmetic Operators
Used for basic math: addition, subtraction, multiplication, division, and modulus. They work with primitive data types like int,
float, double, long, and others.
# Operator Symbol
1 Addition +
2 Subtraction -
3 Multiplication *
4 Division /
5 Modulus %
6 Increment ++
7 Decrement --
1. Addition (+)
The addition operator adds two operands.
public class AdditionExample {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
[Link]("Sum: " + sum);
}
}
Sum: 30
2. Subtraction (-)
The subtraction operator subtracts the right operand from the left operand.
public class SubtractionExample {
public static void main(String[] args) {
int a = 20;
int b = 10;
int difference = a - b;
[Link]("Difference: " + difference);
}
}
Difference: 10
3. Multiplication (*)
The multiplication operator multiplies two operands.
public class MultiplicationExample {
public static void main(String[] args) {
int a = 10;
int b = 20;
int product = a * b;
[Link]("Product: " + product);
}
}
Product: 200
4. Division (/)
The division operator divides the left operand by the right operand. Integer division returns the quotient and discards the
remainder; floating-point division returns a decimal result.
public class DivisionExample {
public static void main(String[] args) {
int a = 20;
int b = 10;
int quotient = a / b;
[Link]("Quotient: " + quotient);
double x = 20.0;
double y = 3.0;
double result = x / y;
[Link]("Result: " + result);
}
}
Quotient: 2
Result: 6.666666666666667
5. Modulus (%)
The modulus operator returns the remainder of the division of the left operand by the right operand.
public class ModulusExample {
public static void main(String[] args) {
int a = 20;
int b = 3;
int remainder = a % b;
[Link]("Remainder: " + remainder);
}
}
Remainder: 2
6. Increment (++)
The increment operator increases the value of a variable by 1. It can be used as a prefix or postfix:
Prefix (++variable): increments the value before using it in an expression.
Postfix (variable++): increments the value after using it in an expression.
public class IncrementExample {
public static void main(String[] args) {
int a = 10;
[Link]("Prefix Increment: " + (++a));
a = 10;
[Link]("Postfix Increment: " + (a++));
[Link]("After Postfix Increment: " + a);
}
}
Prefix Increment: 11
Postfix Increment: 10
After Postfix Increment: 11
7. Decrement (--)
The decrement operator decreases the value of a variable by 1. It can be used as a prefix or postfix:
Prefix (--variable): decrements the value before using it in an expression.
Postfix (variable--): decrements the value after using it in an expression.
public class DecrementExample {
public static void main(String[] args) {
int a = 10;
[Link]("Prefix Decrement: " + (--a));
a = 10;
[Link]("Postfix Decrement: " + (a--));
[Link]("After Postfix Decrement: " + a);
}
}
Prefix Decrement: 9
Postfix Decrement: 10
After Postfix Decrement: 9
Bonus: All Operators in One Program
A single complete, runnable program demonstrating every arithmetic operator together.
public class ArithmeticOperatorsDemo {
public static void main(String[] args) {
int a = 20;
int b = 10;
// Addition
[Link]("Sum: " + (a + b));
// Subtraction
[Link]("Difference: " + (a - b));
// Multiplication
[Link]("Product: " + (a * b));
// Division
[Link]("Quotient: " + (a / b));
// Modulus
[Link]("Remainder: " + (a % 3));
// Increment
int c = 10;
[Link]("Prefix Increment: " + (++c));
c = 10;
[Link]("Postfix Increment: " + (c++));
[Link]("After Postfix Increment: " + c);
// Decrement
int d = 10;
[Link]("Prefix Decrement: " + (--d));
d = 10;
[Link]("Postfix Decrement: " + (d--));
[Link]("After Postfix Decrement: " + d);
}
}
Sum: 30
Difference: 10
Product: 200
Quotient: 2
Remainder: 2
Prefix Increment: 11
Postfix Increment: 10
After Postfix Increment: 11
Prefix Decrement: 9
Postfix Decrement: 10
After Postfix Decrement: 9
Summary
Arithmetic operators are fundamental in performing mathematical calculations in Java programs. Understanding how to use
each operator — and the difference between prefix and postfix increment/decrement — is essential for writing effective and
accurate code.
Source: [Link] — Java Arithmetic Operators ([Link]