View Full Version : One of those Java gotchas....
suryad
05-15-2005, 11:47 PM
Ok please can someone explain if the value 3.14 is a double or a float...I am looking at this Java Sun Certification book and it says that 3.14 is a float and not a double.
The question is as follows:
Which 3 are valid declarations of a float? (Choose 3)
A. float f1 = -343;
B. float f2 = 3.14;
C. float f3 = 0x12345;
D. float f4 = 42e7;
E. float f5 = 2001.0D;
F. float f6 - 2.81F;
I chose the answers to be A, B and F. The book says A, C and F. Can someone explain this to me? I mean I am leaning towards the I am wrong but the book is right kind of deals but something tells me I am right and the book wrong. :eek:
I would say A, C, D, and F (in C/C++ anyway).. best thing you can do is put them into a test program and see which ones make errors :)
suryad
05-16-2005, 01:35 AM
I would say A, C, D, and F (in C/C++ anyway).. best thing you can do is put them into a test program and see which ones make errors :)
Aman thanks for the reply...appreciate it. But can you explain to me why you would pick C? Thanks bud.
TheShadowKnows
05-16-2005, 01:59 AM
Well, I haven't played with java in years, but my intuition tells me this:
* double is a larger storage class than float. Both essentially do the same thing, but double has a higher capacity.
* The compiler is going to choose float over double if it can.
* In the context of declaring a variable (in this case, float [var] (= val)? ), the compiler will choose float (i.e., I don't think the declaration double pi = 3.14; would cause an error).
Honestly, I do not understand why C is wrong if A is correct (both are integers), beyond that the java compiler decides that you screwed up in that only a maniac would assign an integeral value expressed in hexadecimal to a float, expecting an automatic conversion. I don't believe one could actually specify a float literal in hex though (as most languages do not support his), such as 0x0001.2 to mean 1.125 in decimal.
E explicitly defines a double (the D at the end) I believe.
F explicitly defines a float literal (the F at the end), I believe as well.
D is definately bogus as any mantissa that is 10 or greater is invalid in scientific notation. That is if such syntax is accepted to begin with.
suryad
05-16-2005, 05:09 AM
Agreed Shadow, I think that both B and C are valid but the book says B is incorrect simply by stating that B is double...which kinda does make sense if I think with your logic that the language will automatically treat it as a double...but I have used that form before and the compiler did not ***** at me...so I am confused :confused:
I would have thought b was correct, but I tried it and it will not compile.
TheShadowKnows
05-16-2005, 11:57 AM
I tried B as well and it does not compile (under GCJ). Maybe all floating point literals are double by default? Maybe java doesn't like (float d1 = 3.14; ) as it needs to "demote" the type of 3.14. With the integer, it is promoting the type (as in range, not necessarily accuracy). You could make B "float f2 = 3.14f;" to make it correct.
Example C does compile fine on my system. As it should.
<edit>
It is probably for the exact same reason why the following does not compile:
long x = 2;
int y = x;
Java will not implicitly demote types. It will implicitly promote them, as
the following compiles:
int x = 2;
long y = x;
</edit>
shmoove
05-16-2005, 01:13 PM
Maybe all floating point literals are double by default?
That's exactly it.
shmoove
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.