← Chapter 2 — Core Syntax & Data

10. Arithmetic Operators

mixed

Description

Arithmetic operators let programs calculate totals, differences, products, quotients, and remainders. They work naturally with numeric variables.

When both operands are integers, division returns an integer result. Convert one operand to double when a fractional answer matters.

Subtopics

Basic arithmetic

Description

Use +, -, *, and / for common calculations.

Explanation

Parentheses make calculation order explicit.

Example

int total = 4 + 6 * 2;

Remainder operator

Description

The % operator returns the remainder.

Explanation

It is useful for even/odd checks and repeating patterns.

Example

boolean even = 12 % 2 == 0;

Explanation

Operator precedence can surprise beginners. Write parentheses whenever they make a formula easier for another person to verify.

Example

public class ArithmeticDemo {
    public static void main(String[] args) {
        int scored = 82;
        int total = 100;
        double percent = scored * 100.0 / total;
        System.out.println(percent);
    }
}

Key points

  • Integer division discards fractions.
  • % returns a remainder.
  • Parentheses improve readability.

Open reference