Java BasicsCore· 30 min read

Operators (Maths & Comparisons)

Operators let you do maths on numbers and compare values to get true or false.

What you will learn

  • Use the arithmetic operators
  • Compare values to produce a boolean
  • Understand integer division

Arithmetic operators

These do everyday maths. The only surprising one is %, the remainder (also called modulo) — what is left over after division.

OperatorMeansExampleResult
+Add7 + 310
-Subtract7 - 34
*Multiply7 * 321
/Divide7 / 23 (see warning)
%Remainder7 % 31
Arithmetic in action
int a = 7;
int b = 3;
System.out.println("Sum: " + (a + b));
System.out.println("Product: " + (a * b));
System.out.println("Remainder: " + (a % b));

Note: Output: Sum: 10 Product: 21 Remainder: 1 7 divided by 3 is 2 with 1 left over, so 7 % 3 is 1. The remainder operator is handy for checking even or odd numbers later.

Watch out: Integer division surprises everyone: 7 / 2 gives 3, not 3.5, because both numbers are whole (int). To get 3.5, make at least one a decimal: 7.0 / 2.

Order of operations (precedence)

Just like in school maths, Java does *, / and % before + and -. If you want a different order, wrap it in brackets ( ), which always go first.

Multiplication happens before addition unless brackets say otherwise
int result1 = 2 + 3 * 4;      // multiply first
int result2 = (2 + 3) * 4;    // brackets first

System.out.println("Without brackets: " + result1);
System.out.println("With brackets:    " + result2);

Note: Output: Without brackets: 14 With brackets: 20 In the first line Java computes 3 * 4 = 12 first, then adds 2 to get 14. In the second, the brackets force 2 + 3 = 5 first, then multiply by 4 to get 20. When in doubt, add brackets to make the order obvious.

Comparison operators

These compare two values and give back a boolean (true or false). You will use them constantly in if-statements.

OperatorMeans
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
Comparisons return true or false
int score = 72;
System.out.println(score >= 50);
System.out.println(score == 100);
System.out.println(score != 72);

Note: Output: true false false 72 is at least 50 (true), it is not equal to 100 (false), and it IS equal to 72 so not-equal is false. Each comparison gives back a boolean.

Watch out: Use == (two equals) to COMPARE values. A single = ASSIGNS a value. Mixing them up is one of the most common beginner bugs.

Tip: A neat trick: a number is even when number % 2 == 0. The remainder after dividing by 2 is 0 only for even numbers.

Q. What does 7 / 2 give in Java when both values are int?

Answer: Dividing two whole numbers does integer division, which throws away the decimal part — so 7 / 2 is 3. Use 7.0 / 2 to get 3.5.

✍️ Practice

  1. Print whether 13 is even using 13 % 2 == 0.
  2. Set two int variables and print the result of all five arithmetic operators on them.

🏠 Homework

  1. Write a program that stores two numbers and prints their sum, difference, product, division and remainder, each clearly labelled.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →