Task 1 of 14 · 0 solved · 14 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

1.4 Arithmetic — integer division

Lesson

Arithmetic in Java

Java does math with + - * / and the remainder operator %. This module has a few surprises — the first is how division behaves with whole numbers.


Integer division truncates

When both operands are int, / keeps only the whole part and drops the remainder — it does not round. So 7 / 2 is 3, not 3.5 and not 4.


Worked example

System.out.println(7 / 2);       // 3
System.out.println(30 / 4);      // 7
System.out.println(24 / 4 / 2);  // 3  (left to right: 6 / 2)

See it yourself

Run the code below — it prints 3. Now change 7 / 2 to 7 / 2.0 and run again: because one operand is a double, the result becomes 3.5. That one dot changes everything.



▶ Try it: run this, then change something and run it again.



What is 7 / 2 in Java?


Type exactly what this prints:

System.out.println(30 / 4);

What does 24 / 4 / 2 evaluate to?


True or false?