The Five Basic Arithmetic Operators: +, -, *, /, %
You'll learn how to perform addition, subtraction, multiplication, division, and modulus operations in Java using the five fundamental arithmetic operators.
The Core Operators
Java provides five arithmetic operators that work just like the math you learned in school—with a few programming twists:
Addition (+): Adds two values together.
int sum = 10 + 5; // sum is 15
Subtraction (-): Subtracts the right value from the left.
int difference = 10 - 5; // difference is 5
Multiplication (*): Multiplies two values. Notice we use *, not ×.
int product = 10 * 5; // product is 50
Division (/): Divides the left value by the right. Remember: integer division truncates!
int quotient = 10 / 5; // quotient is 2
int truncated = 10 / 3; // truncated is 3, not 3.333...
Modulus (%): Returns the remainder after division. Think of it as "what's left over."
int remainder = 10 % 3; // remainder is 1 (10 ÷ 3 = 3 with 1 left over)
int evenCheck = 10 % 2; // evenCheck is 0 (no remainder = even number)
Understanding Modulus
The modulus operator (%) is less familiar but incredibly useful. If you divide 10 cookies among 3 people, each gets 3 cookies, and you have 1 left over—that leftover is what % gives you. It's commonly used to check if numbers are even/odd or to cycle through values.
All five operators work with any numeric type: int, long, byte, short, float, and double.
Key Takeaway: Java's five arithmetic operators—+, -, *, /, and %—perform addition, subtraction, multiplication, division, and remainder calculations, working just like basic math with numeric types.