File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in casting" Watch "Problem in casting" New topic

Problem in casting

Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
float f1 = 10L;
Float f = new Float(10L);
Float f = new Float(10.00D);
The above codes are working fine. But in K&B(page 377) says Float constructor accepts only float or String!!?.
I thought, eventhough the long and double values are in the range of float, the compiler will not accept it unless otherwise it is declared as final.
But confused now..
any ideas?..
[ January 29, 2004: Message edited by: Mohamed Yasin ]

Regards,<br />Yasin<br />SCJP,SCWCD,SCBCD
Xiao Hu

Joined: Jan 28, 2004
Posts: 9
Hi, Mohamed
Here is source code for all 3 of Float constructors
public Float(float value) {
this.value = value;
public Float(double value) {
this.value = (float)value;
public Float(String s) throws NumberFormatException {
1. a long can be implicitly cast to float, compiler would happy to accept that.
2. a double parameter will be simply cast to float, may cause losing precision.
Hope it helps
Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
Thanks Xiao Hu.
Gian Franco
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Mohamed,
I've checked K&B, because I was remembering two exceptions
for the constructor arguments for wrappers, namely Character
and Float, which respectively have 1 and 3 possible arguments.
All the others have 2.
In table 6-2 it says the Float constructor can accept float,
double and String.
Gian Franco

"Eppur si muove!"
I agree. Here's the link:
subject: Problem in casting
It's not a secret anymore!