🌱BeginnerOperators
Arithmetic Operators
Perform calculations in Java using addition, subtraction, multiplication, division, modulus, and increment/decrement operators.
Overview
Arithmetic operators let you perform mathematical calculations in Java. The five basic ones (+, -, *, /, %) work on numeric types. Java also has two shorthand operators - ++ (increment) and -- (decrement) - that add or subtract 1 from a variable.
Integer division (dividing two ints) drops the decimal part: 7 / 2 = 3, not 3.5. To get a decimal result, cast one operand to double first. The modulus operator (%) returns the remainder after division and is useful for checking divisibility.
Key Points
- •+ addition, - subtraction, * multiplication, / division, % modulus (remainder)
- •Integer division truncates: 7 / 2 = 3 (not 3.5); use (double) cast to get decimals
- •% modulus: 7 % 3 = 1 (remainder); n % 2 == 0 means n is even
- •++ increments by 1: i++ (post-increment) vs ++i (pre-increment)
- •-- decrements by 1: i-- (post) vs --i (pre)
- •Operator precedence: * / % before + - ; use parentheses to control order
Examples
Basic arithmetic
public class Main {
public static void main(String[] args) {
int a = 17, b = 5;
System.out.println(a + b); // 22
System.out.println(a - b); // 12
System.out.println(a * b); // 85
System.out.println(a / b); // 3 ← integer division!
System.out.println(a % b); // 2 ← remainder
// Get a decimal result from two ints
System.out.println((double) a / b); // 3.4
}
}17 / 5 = 3 (remainder 2). The decimal .4 is discarded in integer division.
Increment and decrement
public class Main {
public static void main(String[] args) {
int x = 5;
System.out.println(x++); // 5 - prints THEN increments
System.out.println(x); // 6
int y = 5;
System.out.println(++y); // 6 - increments THEN prints
System.out.println(y); // 6
int z = 10;
z--;
System.out.println(z); // 9
}
}Post-increment (x++) returns the old value. Pre-increment (++x) returns the new value.
Operator precedence and parentheses
public class Main {
public static void main(String[] args) {
System.out.println(2 + 3 * 4); // 14 (* before +)
System.out.println((2 + 3) * 4); // 20 (parentheses first)
// Compound interest: A = P * (1 + r)^n (simplified)
double p = 1000, r = 0.05;
int n = 3;
double amount = p * Math.pow(1 + r, n);
System.out.printf("Amount: %.2f%n", amount); // 1157.63
}
}Always use parentheses when you're unsure about precedence - it makes intent explicit.
Common Mistakes
- Integer division: expecting 5 / 2 to be 2.5 - it is 2 in Java (both operands are int)
- Division by zero with ints: `int x = 5 / 0` throws ArithmeticException; double division gives Infinity
- Confusing x++ with ++x in expressions where the return value matters
Note
Use Math.pow(base, exponent) for exponentiation - Java has no ** operator like Python.
Exercises
Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.
Loading editor…
Completed: 0 / 4 exercises
Knowledge Check
Question 1 of 4