I'm writing a simple program that converts pounds to kilos, but I'm a bit confused
import java.util.Scanner;
public class FiveTenOne {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in); //Creates Scanner Object
System.out.print("Enter your weight in pounds: ");
float pounds = keyboard.nextFloat();
float kilos = pounds / 2.2;
System.out.print("Your value in kilos is: " + kilos);
keyboard.close();
}
}
This is the code I wrote, all variables have been set to floats, yet it tells me that it cant convert from a double. at "pounds / 2.2;"
However when I write it like the code below "(float) (pounds / 2.2)" then no error comes back
import java.util.Scanner;
public class FiveTenOne {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in); //Creates Scanner Object
System.out.print("Enter your weight in pounds: ");
float pounds = keyboard.nextFloat();
float kilos = (float) (pounds / 2.2);
System.out.print("Your value in kilos is: " + kilos);
keyboard.close();
}
}
Can someone explain to me why the first code doesn't take it as a float?
or is it an IDE problem?
I tried both ways and know that the second one works fine, but I want to understand why exactly the first one doesn't work
3 Answers 3
float is, at this point, best thought of as a historic abberation. Do not use them; there is no need. double is like float but more precise, and just as fast if not faster. Also, java is designed to prefer them, so simple constants such as 2.2 are actually considered a double. Hence, aFloatValue / 2.2 is a float divided by a double, which java solves by first silently upgrading the float to a double, then doing double division. This produces a double value. Which you try to assign to a variable of type float which is risky (can remove detail - a so called 'narrowing conversion'), which requires an explicit cast so you, the programmer, can tell the compiler: Yes. yes, I heard you, this is dangerous, I know what I'm doing shut up and compile my code please.
The correct answer here is to get rid of all mention of the word float and use double everywhere. You lose nothing, and your code is simpler; no casts would be needed then.
Comments
Double and Float are the datatypes in java, so to convert from one type to the other you have to cast it .
For example , pounds/2.2 returns a double value ( one class ) say 20.0.
To convert it we have to cast into Float which is 20f.
1 Comment
pounds/2.2 returns a double value" This would be a much better answer if you explained why that is the case.double and float are different data types, see e.g. Java: double vs float. The error message says that the division pounds / 2.2 returns a double value. If you want the result to be float, the value has to be cast to float explicitly, just like you did in your second code example.
1 Comment
double - a primitive - with Double - a wrapper class!
2.2is adouble(not afloat) and if one of the operands isdouble, the result will bedouble- explained in the Java Language Specification 5.6. Numeric Contexts. See also 3.10.2. Floating-Point Literals.