Bifalgorithm post · Computer science one · Basic I/O

How does Java handle assignments between floating-point and integer variables?

How does Java handle assignments between floating-point and integer variables? (Computer science one · Basic I/O) — "Assigning a floating point number to an in…

Ideas in this flowchart

  • Java requires explicit casts when assigning floating-point values to integers.
  • Java does not support implicit type casts.
  • Assigning a floating-point number to an integer without a cast causes a compiler error.
  • An explicit cast allows the floating-point expression to be assigned to an integer.
  • The explicit cast can be written as d = (int) (b + y).
  • The expression b + y is computed as 20 + 3.4 = 23.4.
  • Casting 23.4 to int truncates the fractional part .4.
  • After truncation, d receives the value 23.
  • Assigning an int value to a double variable is allowed.
  • The integer 2 becomes the floating-point value 2.0.
Feed/

How does Java handle assignments between floating-point and integer variables?

No cover

Channel

Computer science one

1 page

"Assigning a floating point number to an integer is not allowed in Java and attempting to do so will be treated as a compiler error. This is because Java does not support implicit type casts. However, you can do so if you provide an explicit type cast as in the code above, d = (int) (b + y); In this code, b + y is correctly computed as 20 + 3 . 4 = 23 . 4, but the explicit type cast (down to an integer) results in truncation. The . 4 gets cutoff and d gets the value 23. Assigning an int value to a double variable is not a problem as the integer 2 becomes the floating point number 2 ."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.