# Thread: Help with valid declarations of a float

1. Member Join Date
Jul 2007
Posts
40
Rep Power
0

## Help with valid declarations of a float

Hi, Ok please can someone explain if the value 3.14 is a double or a float...I am looking at this Java Sun Certification book and it says that 3.14 is a float and not a double.

The question is as follows:
Which 3 are valid declarations of a float? (Choose 3)
• A. float f1 = -343;
• B. float f2 = 3.14;
• C. float f3 = 0x12345;
• D. float f4 = 42e7;
• E. float f5 = 2001.0D;
• F. float f6 - 2.81F;

I chose the answers to be A, B and F. The book says A, C and F. Can someone explain this to me? I mean I am leaning towards the I am wrong but the book is right kind of deals but something tells me I am right and the book wrong.
Thanks.  Reply With Quote

2. ## If a number has a decimal it is a double. To make such a number a float you can do
Java Code:
```float f = (float)3.14;  // cast
// or
float c = 3.14f;  // or 3.14F```
And any number of lesser precision can be assigned to a reference of higher precision.
Java Code:
```float f = 25;
double d = 15f;
long b = 88;  // int```
To see what java says:
Java Code:
```class FloatTest {
public static void main(String[] args) {
check(-343);
check(3.14);
check(0x12345);
check(42e7);
check(2001.0D);
check(2.81F);
}

private static void check(Number n) {
System.out.println(n + " = " + n.getClass().getName());
}
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•