Difference in Java casting

Asked

Viewed 134 times

2

What is the difference between the 2 codes below?

first

int x = 10;
float i = (float) x;

2nd

int x = 10;
float i = Float.parseFloat(x);

1 answer

3


It’s very simple, the first works and the second doesn’t, it doesn’t make sense to make a Parsing in an integer number, only in strings. So documentation shows that only one string.

Alias the method name is a "prime":P

The first indicates that a number that was integer must be interpreted as a type of binary floating point. There will be a conversion because the formatted ones of each type is different. In fact it works without it, there is an implicit conversion of int for float in accordance with language specification.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.