Casting, Overflow &
Code Shortcuts

Run the rules, explain the output, and rewrite code with confidence.

Task 1: Casting Test

Observe what happens when a double is forced into an int

DataConversionPractice.java
java

Task 2: Overflow Test

Push an int past its maximum value and study the result

OverflowCheck.java
java
OverflowCheck.java
java

Task 3: Code Shortcuts

Rewrite the same logic with compound operators and ++

RewritePractice.java
java
Goal:
  • 1. Keep the starting value the same.
  • 2. Replace the long arithmetic assignments with shortcut operators.
  • 3. Use ++ for the final + 1.
  • 4. Make sure the printed result stays exactly the same.

Rewrite Challenge

Rewrite the code using +=, -=, *=, and ++ without changing the final output.

Task 4: Parentheses Matter

Compare two similar lines and explain why they produce different results

ParenthesesCheck.java
java

Bonus Challenge

Money Conversion

Convert a decimal dollar amount into cents using a safe rounding strategy.

  • 1. Start with a double amount such as 1.99.
  • 2. Convert it into cents using an int.
  • 3. Use a rounding step before casting so the result stays accurate.
  • 4. Print the original amount and the final cents.