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

Java Programming Objects and Collections Difficulty: Easy
Choose an option
  • A
    float f = 1F;
  • B
    float f = 1.0;
  • C
    float f = "1";
  • D
    float f = 1.0d;

Answer

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;

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