Java Programming - Objects and Collections - Discussion

Discussion Forum : Objects and Collections - General Questions (Q.No. 9)
9.
Which is valid declaration of a float?
float f = 1F;
float f = 1.0;
float f = "1";
float f = 1.0d;
Answer: Option
Explanation:

Option A is valid declaration of float.

Option B is incorrect because any literal number with a decimal point u declare the computer will implicitly cast to double unless you include "F or f"

Option C is incorrect because it is a String.

Option D is incorrect because "d" tells the computer it is a double so therefore you are trying to put a double value into a float variable i.e there might be a loss of precision.

Discussion:
2 comments Page 1 of 1.

Harper said:   8 years ago
@Prince: both are right.

The computer will implicitly cast to double but then it will be cast to float. Therefore f gets float value.

But why is B wrong?

Prince said:   9 years ago
If I put 1.0f; or 1f;

Which is right?

Post your comments here:

Your comments will be displayed after verification.