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 1 of 4.

RAHUL CHARAN said:   9 years ago
Hi, @Shubham.

42e7 = 420000000.

Range of int = - 2147483648 to 2147483647 that means 42e7 < range of int, so we can put this value in int.

Now int x= 42e7;
If float f =x;

The rule is:
byte->short->int->long->float->double.
char->int.
Left to right is promotions are acceptable.

So, we can put int Val in float Val that's how it can be assigned to float even long value we can assign in float though long is of 8 bytes and float is 4 bytes but internal structures are different.

And e7 = 10^7;
It's exponential form.
(1)

Abhishek said:   6 years ago
343 is an integer and in Java automatic type conversion takes place which is also called implicit type conversion through which an integer may be stored as a floating point literal in a float type variable.

In other words;

int a=5;
will store 5 as an integer value in int a.

but:
float k=a;
will convert 5 to 5.0f and store it in k.
(3)

Sundar said:   1 decade ago
Hi All,

Why 3.14 is not a float number?

According to Java compiler, it is not a float number. It is a double value.

If you want the java compiler to deal it as float value you have to specify as given below:

float f = 3.14f;

Hope this will help you. Have a nice day!
(1)

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)

Ramesh polaveni said:   8 years ago
Floating point variables have a precision of 6 digits whereas the precision of double is 14 digits.

In the question, f=3.14 which is more than 6 digits after the decimal. So it's not a float value. It belongs to double.
(1)

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.

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)

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 !

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.

Zubair said:   8 years ago
Hello, @Sulemankhan.

Everything written like float f2=3.14 is always double until it declared like float f2 =3.14f.


Post your comments here:

Your comments will be displayed after verification.