Misconception: When students use the division operator during declaration or assignment of a Double in Java, like double x = a/b, they forget about integer division, which can result in rounding errors.

  • Emphasize to students that the right side of an assignment statement is executed first to help them understand integer-division rounding errors.
    • In the following Java code, students may incorrectly think that x = 0.5
      • int a = 1;
      • int b = 2;
      • double x = a/b;
    • Changing the final statement above so that at least one of the int variables has a double wrapper corrects this mistake.
  • This misconception is the result of a few misunderstandings, namely how different types of primitives interact with one another, what order the code is executed in, and what the different modes of the arithmetic operator for division, /, are.
    • Note from the CS Teaching Tips Team: To learn more about this rounding error in Java, check out the Division entry in CMP’s Java Glossary.

More about this tip

External Source
Interview with Helen Hu.
Other Tips By