My Reflection

Q18 Print statement with mathematical operators Q18

I was surprised I got this question wrong, but again it was because I was rushing. The first operation that is executed is 404/10. This returns 40 NOT 40.4 since they are both integers going through integer division. Then the value of 40 will be multiplied by 10, resulting in 400. 400 + 1 = 401 which is printed as the final answer.

There were just a lot of things at play that were tricky about this especially that the 404/10 != 40.4. Multiplication and division have the same priority so the order of operations was not much of a struggle for me.

  1. Numerical Data Types: In Java, numerical data types represent numbers. The two main categories are integers and floating-point numbers.
  • Integers: Whole numbers without decimal points. Examples include int and long.

  • Floating-point: Numbers with decimal points. Examples include float and double.

  1. Mathematical Operations: Java supports a variety of mathematical operations that you can perform on numerical data types.

Addition (+): Combines two numbers.

int result = 5 + 3; // Result will be 8

Subtraction (-): Finds the difference between two numbers.

int result = 7 - 4; // Result will be 3

Multiplication ():* Multiplies two numbers.

int result = 6 * 2; // Result will be 12

Division (/): Divides one number by another.

double result = 10.0 / 3.0; // Result will be 3.3333...

Modulus (%): Returns the remainder after division.

int result = 15 % 4; // Result will be 3

How do number types make the outcome of series of operations differ?

Explain the difference in the output of the two operations results below :

int resultInt = (403 / 9 * 10 + 1);
double resultDouble = (403.0 / 9 * 10 + 1);

// System.out.println(resultInt);
// System.out.println(resultDouble);

Try removing the comments and running it… Notice how these results differ despite having the same operations and numbers…

  1. Using Integers (int):
int resultInt = (403 / 9 * 10 + 1);
  • Division (403 / 9): Integer division results in 44 (since it discards the decimal part).
  • Multiplication (44 * 10): The result is 440.
  • Addition (440 + 1): The final result is 441.
  1. Using Doubles (double):
double resultDouble = (403.0 / 9 * 10 + 1);
  • Division (403.0 / 9): Floating-point division results in approximately 44.7778.
  • Multiplication (44.7778 * 10): The result is approximately 447.778.
  • Addition (447.778 + 1): The final result is approximately 448.778.

Summary:

  1. Integers (int):
  • Experience truncation during division (decimal part discarded).
  • May result in rounding down or loss of precision.
  • Final result is exact within the range of integers.
  1. Doubles (double):
  • Preserve decimal precision during division.
  • More accurate results in floating-point operations.
  • Final result may have a decimal part, providing a more precise answer.

Key Takeaway:

  1. Integer Operations:
  • Rounding or truncation occurs during division.
  • Exact within the range of integers but may lead to loss of precision.
  1. Floating-Point Operations:
  • Maintain decimal precision during operations.
  • Provide more accurate results but might introduce rounding errors.

Understanding how each type of number behaves during operations is important. Integers may be exact within their range but could lose precision, while doubles preserve decimal precision, offering more accurate results despite the potential for rounding errors. Choosing the appropriate type depends on the nature of the calculations and the desired level of precision.