Java Arithmetic Operators
Table of Contents + โ
In the last lesson you learned about Java autoboxing and unboxing. Real programs add up a bill, split a cost, or check if a number is even. For all of that, Java gives you arithmetic operators, the symbols that do the math for you.
โ The basic math operators
Java has five arithmetic operators. Four come from school. The fifth, %, is new to most people and turns out to be one of the most useful.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Add | 5 + 2 | 7 |
| - | Subtract | 5 - 2 | 3 |
| * | Multiply | 5 * 2 | 10 |
| / | Divide | 6 / 2 | 3 |
| % | Modulus (remainder) | 7 % 2 | 1 |
The symbols differ from your math book, just because of keyboards. The meaning is the same.
*means multiply, not the times sign./means divide, not the line with two dots.
This program stores two numbers, then prints the result of each operator.
int a = 10;int b = 3;System.out.println(a + b); // addSystem.out.println(a - b); // subtractSystem.out.println(a * b); // multiplySystem.out.println(a / b); // divideEach line applies one operator. Add gives 13, subtract gives 7, multiply gives 30. Watch the last division line, it does not give what most people expect.
Output
137303โ Watch out for integer division
10 / 3 gave 3, not 3.33. Divide two int values and Java does integer division.
- It keeps only the whole part and drops the decimal. This is called truncation.
- It never rounds:
7 / 2truncates to3, not4. - So
10 / 3is3. 3 goes into 10 three times, the1left over is ignored.
To get the decimal, at least one side must be a double. Cast one first, like in the casting lesson.
int a = 10;int b = 3;double result = (double) a / b; // โ
one side is now a doubleSystem.out.println(result);Casting a to double switches Java to floating-point division, so the decimal comes back.
Output
3.3333333333333335The stray 5 at the end is normal. A double cannot store a repeating decimal perfectly, so it gets close and stops. More on that quirk later.
Where you cast matters too.
double wrong = (double) (10 / 3); // โ divides first, then castsdouble right = (double) 10 / 3; // โ
casts first, then dividesSystem.out.println(wrong);System.out.println(right);See the trap?
- First line:
10 / 3runs as integer division and gives3, then casting makes it3.0. The decimal is already gone. - Second line: the cast happens before the division, so it runs in decimal.
- The rule: cast a value, not the finished result.
Output
3.03.3333333333333335Two ints always give an int
int / int always gives an int, with the decimal cut off, not rounded. If you need a decimal result, make sure at least one side is a double before the division runs.
๐งฎ The modulus operator
The % operator gives the remainder, the part left over after dividing.
7 % 2is1. 2 goes into 7 three times (uses up 6), and1is left over.- It does not mean percentage. Same symbol, totally different job.
This program prints the remainder for three divisions.
System.out.println(7 % 2); // 1 left overSystem.out.println(10 % 5); // nothing left overSystem.out.println(14 % 4); // 2 left overRead each as โwhat is left overโ. 7 over 2 leaves 1. 10 over 5 leaves 0. 14 over 4 leaves 2.
Output
102Small as it looks, % shows up everywhere in real code.
Checking even or odd. The classic use. An even number leaves no remainder when divided by 2, an odd one leaves 1. So check the remainder against zero.
int number = 8;if (number % 2 == 0) { System.out.println("Even");} else { System.out.println("Odd");}If number % 2 is 0, nothing is left over, so the number is even. More on if statements soon.
Output
EvenGetting the last digit. Any number % 10 gives its last digit, since the last digit is the remainder when you divide by 10. This program pulls the last digit out of 2025.
int year = 2025;System.out.println(year % 10); // โ
last digitSo 2025 % 10 is 5. Handy when you need to break a number apart digit by digit.
Output
5Wrapping around. On a 12-hour clock you want the hour to loop back to 0 after 11. The % does that wrap. This program shows the hour after adding 5 to hour 10.
int hour = (10 + 5) % 12; // โ
wraps back inside 0 to 11System.out.println(hour);10 plus 5 is 15, but a clock only has 0 to 11. So 15 % 12 is 3, wrapping to 3. The same idea keeps an index inside an array or moves a player around a board.
Output
3โโ Increment and decrement
Adding or taking away 1 is so common Java gives it a shortcut, called increment and decrement.
++adds 1 to a variable.--takes 1 away.
This program shows both.
int score = 5;score++; // now 6score--; // back to 5System.out.println(score);So score++ is a short way of writing score = score + 1.
Output
5You can write ++ before or after the variable. Both add 1, but they differ in what the expression hands back at that moment.
- Postfix, like
x++, gives back the old value first, then adds 1. - Prefix, like
++x, adds 1 first, then gives back the new value. - On its own line it makes no difference. The difference only shows when you use the value in the same statement.
This program makes it clear.
int x = 5;System.out.println(x++); // prints 5, then x becomes 6System.out.println(x); // prints 6
int y = 5;System.out.println(++y); // y becomes 6 first, then prints 6So x++ printed 5, the old value, then x turned into 6. But ++y added first, so it printed 6.
Output
566When in doubt, keep it simple
If you are not sure whether you need prefix or postfix, put the ++ or -- on its own line. Then both behave the same, and your code stays easy to read.
๐ Mixing int and double
When an int meets a double in the same calculation, Java uses a rule called type promotion.
- The smaller type is lifted up to match the bigger one before the math runs.
- If one side is a
double, the other becomes adoubletoo. - So the whole calculation runs in decimal, and the result is a
double.
This program multiplies an int by a double.
int count = 3;double price = 2.5;double total = count * price; // int is promoted to doubleSystem.out.println(total);price is a double, so Java promotes count to 3.0, then multiplies. 3.0 * 2.5 is 7.5.
Output
7.5This is why one cast can fix integer division. The moment one side becomes a double, the whole division is promoted to decimal, so casting just one side is enough.
๐ข Order of operations
When you mix operators in one line, Java does not go left to right. It follows precedence, the same order as math class. Multiply, divide, and modulus all happen before add and subtract.
This program looks like it should add first, but watch.
int result = 2 + 3 * 4;System.out.println(result);You might read this as (2 + 3) * 4 = 20. But Java does the * first, so it means 2 + (3 * 4), giving 14.
Output
14The % sits at the same level as * and /. So in 10 + 7 % 3, the % runs first: 7 % 3 is 1, then 10 + 1 is 11.
To control the order yourself, use brackets. Anything inside brackets happens first. This program forces the addition to go first.
int result = (2 + 3) * 4;System.out.println(result);Now the brackets force the addition first. 2 + 3 is 5, then 5 * 4 is 20.
Output
20โ ๏ธ Common Mistakes
These arithmetic traps catch beginners all the time.
Expecting a decimal from int division. The top trap. 5 / 2 gives 2, not 2.5. Both sides are int, so the decimal is cut off.
// โ Wrong: both sides are int, so the decimal is lostdouble avg = 5 / 2; // avg is 2.0, not 2.5
// โ
Right: cast one side to double firstdouble avg2 = (double) 5 / 2; // avg2 is 2.5avg is a double, so you might expect 2.5. But 5 / 2 runs as int math first and gives 2, then stores it as 2.0. The decimal was gone before it reached the double.
Dividing by zero with whole numbers. Dividing an int by 0 does not give infinity. It crashes the program with an ArithmeticException. So always make sure the divider is not zero.
// โ Wrong: this throws an ArithmeticException and stops the programint x = 10 / 0;
// โ
Right: check before dividingint divider = 0;if (divider != 0) { int x = 10 / divider;}A surprising twist: double divide by zero does not crash.
10.0 / 0.0givesInfinity.0.0 / 0.0givesNaN, which means โnot a numberโ.- So whole numbers crash, but decimals quietly give these special values.
This program shows both decimal cases.
System.out.println(10.0 / 0.0); // InfinitySystem.out.println(0.0 / 0.0); // NaNOutput
InfinityNaNForgetting precedence. 2 + 3 * 4 is 14, not 20. The * runs before the +. Use brackets when you want a different order.
โ Best Practices
A few habits keep your arithmetic clean and bug-free.
- Use brackets for clarity. Even when precedence is already correct, brackets make the order obvious.
2 + (3 * 4)reads better than2 + 3 * 4. - Cast for decimals. Dividing whole numbers but want a decimal? Cast one side to
doublefirst, and cast the value, not the finished division. - Guard against divide by zero. Check the divider is not zero before dividing whole numbers. An
intdivide by zero crashes the program. - Keep
++simple. When prefix versus postfix does not matter, put the increment on its own line.
๐งฉ What Youโve Learned
Letโs recap what you can now do.
- โ
Java has five arithmetic operators:
+,-,*,/, and%. - โ
Integer division (
int / int) cuts off the decimal, so10 / 3is3. Cast one side todoublefor decimals. - โ
The modulus
%gives the remainder, handy for even or odd, last digit, and wrap-around. - โ
++and--add or remove 1, and prefix versus postfix changes the value handed back. - โ
Mixing
intanddoubletriggers type promotion, so the result is adouble. - โ
Integer divide by zero crashes, but
doubledivide by zero givesInfinityorNaN. - โ
Java follows precedence:
*,/, and%run before+and-. Use brackets to control the order.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does 10 / 3 give when both are int values?
Why: int / int is integer division, which keeps only the whole part and drops the decimal, so 10 / 3 is 3.
- 2
What does the % operator do?
Why: The modulus operator % returns the remainder, so 7 % 2 is 1.
- 3
What is the value of 2 + 3 * 4 in Java?
Why: Multiply has higher precedence than add, so it is 2 + 12 = 14.
- 4
What happens when you divide an int by zero, like 10 / 0?
Why: Integer divide by zero throws an ArithmeticException and stops the program. Only double divide by zero gives Infinity or NaN.
๐ Whatโs Next?
You can do math now. Next, letโs look at a faster way to update a variable with its own value, using assignment operators like += and -=.