This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I keep encountering an error in my programs whenever I have float variables. In debugging the error usually says something along the lines of found a double required a float. I thought if I had a floating point result and I used floating point variables I would get a float as out put i.e float = float * float.
C:\Java\EigeneJavaProgramme>javac GrossPay1.java GrossPay1.java:4: possible loss of precision found : double required: float float hoursWorked = 3.056; ^ GrossPay1.java:5: possible loss of precision found : double required: float float workRate = 4.0125; ^ GrossPay1.java:9: possible loss of precision found : double required: float yourPay = ((hoursWorked - 40) * (workRate * 1.5)) + (40 * workRate); ^ 3 errors Why is double found? The input of the yourPay calculation is float so the output to yourpay should Be float, too and not double. Thank you very much for your answers. Thomas
Note the "1.5", that's treated as a double, so the whole result returns a double. Use 1.5f instead.
Joined: Mar 22, 2002
Hi Thomas. The primitive double is like the Borg among other primitive types -- any primtive type that interacts with a double will return a double. For example, double * float double * long double * int double * short double * byte' will return a double result.
Marilyn de Queiroz
Joined: Jul 22, 2000
Put 'F' after each literal and it will work. If you have an expression which includes a double and a float, the float will be automatically promoted to a double.