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.

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)

Asif Khan said:   7 years ago
f1=-343.

Because float ranges from 4.9e-324 to 3.4e+038.

Divya Prakash said:   7 years ago
How f1=-343 is float? Can anyone explain it please?
(2)

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)

Ranjith said:   8 years ago
@Sundar.

Super, your explanation was amazing!.

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.

Sachin Shekhar said:   8 years ago
float f1 = -343;
float f3 = 0x12345;

How 1 &3 are float?

Please explain anyone. I am not able to get it.

Sulemankhan said:   9 years ago
Why float f2=3.14 comes in double not under float?

Siddhesh said:   9 years ago
According to me, float f6 = 2.81F; is a valid declaration.

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)


Post your comments here:

Your comments will be displayed after verification.