Java — Which is a valid declaration of a float value?

Difficulty: Easy

Correct Answer: float f = 1F;

Explanation:


Introduction / Context:
This evaluates the correct way of declaring floating-point literals in Java.



Concept / Approach:

  • By default, floating literals like 1.0 are treated as double in Java.
  • To assign to a float, append f or F.
  • d or D indicates double explicitly.
  • String literals like "1" cannot be assigned directly to float.


Step-by-Step:

Option A: Correct. "1F" explicitly declares a float.Option B: Incorrect. 1.0 defaults to double.Option C: Invalid type mismatch.Option D: Declares double literal with d.


Final Answer:
float f = 1F;

More Questions from Objects and Collections

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion