Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 12)
12.
Which three are valid declarations of a float?
  1. float f1 = -343;
  2. float f2 = 3.14;
  3. float f3 = 0x12345;
  4. float f4 = 42e7;
  5. float f5 = 2001.0D;
  6. float f6 = 2.81F;
1, 2, 4
2, 3, 5
1, 3, 6
2, 4, 6
Answer: Option
Explanation:

(1) and (3) are integer literals (32 bits), and integers can be legally assigned to floats (also 32 bits). (6) is correct because (F) is appended to the literal, declaring it as a float rather than a double (the default for floating point literals).

(2), (4),and (5) are all doubles.

Discussion:
36 comments Page 2 of 4.

Chinmayee said:   1 decade ago
Its true by default any decimal number is treated as a double which is 8 bytes that cannot be assigned to a datatype having 4 byte hence explicit casting is required.

float f=3.14; //compilation error
float f=3.14f; //explicit casting
(1)

Abcd said:   1 decade ago
How can we differentiate whether the value is float or double?

Ankit said:   1 decade ago
By default java compiler takes any real number as a double, its on you and bit depend on the range of the numbers to declare it float. So to get more precise value java takes it double.

ABC said:   1 decade ago
How can we decide value is float or double ? And why -343 is float?

Pria said:   1 decade ago
A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double.

Aadil Farooqui said:   1 decade ago
As it was in my knowledge, when assigning 'f' to a float value, we can only use the lowercase value 'f' not 'F'?

Can someone explain this please?
(2)

Raj said:   1 decade ago
Why 42e7 is not a float value?

Shalini Singh said:   1 decade ago
Please tell me how the following is correct ?

float f1 = -343;

Kayu said:   1 decade ago
How can we say 42e7 as a double?

Abi said:   1 decade ago
How the following declaration is correct in float?

float f1=-343;


Post your comments here:

Your comments will be displayed after verification.