| Author |
Why is "float f4 = 42e7" not considered a valid declaration of a float in Java?
|
Nilay Jhaveri
Greenhorn
Joined: Jun 11, 2006
Posts: 3
|
|
Hi Friends, I was reading the Java Sun Certification Book and one of the sample questions in the first chapter is: " 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; " The Book says the answer is A, C, and F. It is clear to me why F is one of the answers since the "F" suffix is attached to the end of the number. It is also clear why A and C are correct based on the explanation at the end of the chapter which says: "A and C are integer literals (32 bits), and integers can be legally assigned to floats (also 32 bits.)" But what I don't understand is why D is not one of the correct answers, based upon the same logic which makes A and C correct. Specifically, isn't D (42e7) and integer literal as well, making it able to be legally assigned to a float. Since 42e7 represents (240,000,000) , isn't it an int and not a double by default as a literal? Thank you for your help. Nilay
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
No 42e7 is a double.
|
 |
Nilay Jhaveri
Greenhorn
Joined: Jun 11, 2006
Posts: 3
|
|
|
Can you explain why its a double given that it is an int and in the solution they said ints can be assigned to floats?
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
No, it is not an int. When you express a number in scientific notation, it is considered a double unless you add a f or F. Note what happens when you try to compile this. [ June 11, 2006: Message edited by: Keith Lynn ]
|
 |
 |
|
|
subject: Why is "float f4 = 42e7" not considered a valid declaration of a float in Java?
|
|
|