Java Programming - Language Fundamentals - Discussion
Discussion Forum : Language Fundamentals - General Questions (Q.No. 12)
12.
Which three are valid declarations of a float?
- float f1 = -343;
- float f2 = 3.14;
- float f3 = 0x12345;
- float f4 = 42e7;
- float f5 = 2001.0D;
- float f6 = 2.81F;
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 3 of 4.
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?
Can someone explain this please?
(2)
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.
ABC said:
1 decade ago
How can we decide value is float or double ? And why -343 is float?
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.
Abcd said:
1 decade ago
How can we differentiate whether the value is float or double?
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
float f=3.14; //compilation error
float f=3.14f; //explicit casting
(1)
Abc said:
1 decade ago
ox12345 is a hexadecimal number,its decimal form is 74565,which is within the range 2^32,may be so it's represented as float !
Rashmi upadhyay said:
1 decade ago
Hey please tell me. Why option 3 is declared as float?
Yaz said:
1 decade ago
thnks sundar
SALISU said:
1 decade ago
Why 3 is declared as a float.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers