three-thirteen
three-thirteen

Reputation: 1

Moving the contents of a float to a long in Java

The example given for this problem on the site is:

"To move the contents of a variable declared as data type float into a variable declared as data type long requires the use of _____."

Answer: a type cast

Explanation: The explicit type cast "float" is required

long wayHome = 123456789;

float myBoat = (float) wayHome;

But this appears to be doing the opposite to me -- moving a long into a float, not the other way around.

Am I wrong, or is the question wrong? Would it be implicit or typecast?

Upvotes: 0

Views: 130

Answers (1)

Mureinik
Mureinik

Reputation: 312404

The example given is indeed the opposite of what's described in the question's text, like you assumed. However, assigning a float value to a long variable will also require an explicit cast, e.g.:

float myBoat = 123.456

long wayHome = (long) myBoat;

Upvotes: 3

Related Questions