Difference in casting in Java

2

What's the difference between the 2 codes below?

1st

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

2nd

int x = 10;
float i = Float.parseFloat(x);
    
asked by anonymous 26.01.2018 / 16:48

1 answer

3

It's very simple, the first works, and the second does not, it does not make sense to do a parsing in an integer, just in strings . Then the documentation shows that you can only pass one string .

Alias the method name is a "primor": P

The first one indicates that a number that was integer should be interpreted as a binary floating-point type. There will be a conversion because the formatted of each type is different. It actually works without this, there is an implicit conversion from int to float as language specification .

    
26.01.2018 / 16:51