Java Arithmetic Operators

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); // add
System.out.println(a - b); // subtract
System.out.println(a * b); // multiply
System.out.println(a / b); // divide

Each 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

13
7
30
3

โž— 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 / 2 truncates to 3, not 4.
  • So 10 / 3 is 3. 3 goes into 10 three times, the 1 left 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 double
System.out.println(result);

Casting a to double switches Java to floating-point division, so the decimal comes back.

Output

3.3333333333333335

The 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 casts
double right = (double) 10 / 3; // โœ… casts first, then divides
System.out.println(wrong);
System.out.println(right);

See the trap?

  • First line: 10 / 3 runs as integer division and gives 3, then casting makes it 3.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.0
3.3333333333333335

Two 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 % 2 is 1. 2 goes into 7 three times (uses up 6), and 1 is 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 over
System.out.println(10 % 5); // nothing left over
System.out.println(14 % 4); // 2 left over

Read each as โ€œwhat is left overโ€. 7 over 2 leaves 1. 10 over 5 leaves 0. 14 over 4 leaves 2.

Output

1
0
2

Small 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

Even

Getting 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 digit

So 2025 % 10 is 5. Handy when you need to break a number apart digit by digit.

Output

5

Wrapping 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 11
System.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 6
score--; // back to 5
System.out.println(score);

So score++ is a short way of writing score = score + 1.

Output

5

You 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 6
System.out.println(x); // prints 6
int y = 5;
System.out.println(++y); // y becomes 6 first, then prints 6

So x++ printed 5, the old value, then x turned into 6. But ++y added first, so it printed 6.

Output

5
6
6

When 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 a double too.
  • 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 double
System.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.5

This 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

14

The % 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 lost
double avg = 5 / 2; // avg is 2.0, not 2.5
// โœ… Right: cast one side to double first
double avg2 = (double) 5 / 2; // avg2 is 2.5

avg 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 program
int x = 10 / 0;
// โœ… Right: check before dividing
int divider = 0;
if (divider != 0) {
int x = 10 / divider;
}

A surprising twist: double divide by zero does not crash.

  • 10.0 / 0.0 gives Infinity.
  • 0.0 / 0.0 gives NaN, 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); // Infinity
System.out.println(0.0 / 0.0); // NaN

Output

Infinity
NaN

Forgetting 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 than 2 + 3 * 4.
  • Cast for decimals. Dividing whole numbers but want a decimal? Cast one side to double first, and cast the value, not the finished division.
  • Guard against divide by zero. Check the divider is not zero before dividing whole numbers. An int divide 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, so 10 / 3 is 3. Cast one side to double for 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 int and double triggers type promotion, so the result is a double.
  • โœ… Integer divide by zero crashes, but double divide by zero gives Infinity or NaN.
  • โœ… 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. 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. 2

    What does the % operator do?

    Why: The modulus operator % returns the remainder, so 7 % 2 is 1.

  3. 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. 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 -=.

Java Assignment Operators

Share & Connect