Skip to main content

Section 6.3 Converting Integers to Floats

Casting an integer to a float effectively changes the data type of the stored value from an int to a foat. Whereas the integer 5 is represented in the computer’s memory using binary 2’s complement for example, when you write (float)5 this resulting number is stored as mantissa and exponent in the way we just learned.

Check Your Understanding Check Your Understanding

1.

Does the datatype of a variable change when you cast or is it just the number itself that is stored differently inside the computer? For example, if \(a\) and \(b\) are variables of type int, where a has the value 12, b has the value 4, what is the data type of the variable a after the operation ((float)a+b)/3 is performed?
  • int
  • Correct!
  • float
  • Not quite - try again!
  • long
  • Not quite - try again!
  • floating int
  • Not quite - try again!
Hint.
Does the data type of a change in this process?