aspose file tools*
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
Author

Problem in casting

Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
Hi,
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
Greenhorn

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 {
this(valueOf(s).floatValue());
}
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
blacksmith
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.
Greetings,
Gian Franco


"Eppur si muove!"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in casting