A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Programmer Certification (SCJP/OCPJP)
why won't "float x = 1.0;" compile
Joined: Aug 11, 2004
Nov 18, 2004 20:41:00
I understand that
float x = 1.0; // generates a compile error
double x = 1.0; // compiles
But does anyone understand the reasoning behind this. Why isn't 1.0 considered to be an acceptable value for float types??
Joined: Sep 09, 2004
Nov 18, 2004 20:43:00
Default type of values having decimal point is double. So, 1.0 is double and not float.
"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Joined: Jul 05, 2004
Nov 18, 2004 20:46:00
The compiler takes floating point literals as Double by default. That is why it shows an error. We can specify it as a float by adding 'f'.
like float x = 1.0f; Here we tell the compiler that this is a float, so dont take as a double.
Joined: Mar 13, 2004
Nov 18, 2004 21:19:00
However "float x = 1;" using an integer literal compiles OK.
javac is concerned that converting a double value to a float value will lose precision.
You would need either "float x = 1.f;" or "float x = (float)1.;
SCJP 1.4, SCWCD in process
subject: why won't "float x = 1.0;" compile
Java Primitive Literals
Implicit casting Q
Funny little thing...
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014