wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wrappers 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 "wrappers" Watch "wrappers" New topic
Author

wrappers

Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
created test scenario


I expected line 6 and line 4 to fail but why does it work


SCJP 1.4 <br />SCWCD 1.4 (preparing)<br />SCBCD 1.3 (coming soon)
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
sorry guys I found the answer to this, constructor is overloaded to accept double. But why does it still accept float? implicit casting?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
class Float has 3 constructors. One takes a String argument and parses it. The second comstructor takes a float and the third takes a double.

In converting an integer type, the "Float(float value)" constructor will be invoked because it has the most restrictive parameter type that still accepts an integer type. float is more restrictive than double because a float argument will be accepted as a double parameter without an explicit cast, while a double argument will not be accepted as a float parameter without an explicit cast.

The concept of a widening cast has to do with the range of legal values, not precision. So any integer type can become a float as a widening conversion, no explicit cast required. It's true that a double would preserve more precision from an int or long, but Java only considers range.


Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wrappers