This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wrappers" Watch "wrappers" New topic


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:
subject: wrappers
Similar Threads
Implicit Narrowing and Casting
Primitive Casting Exercise
Implict Cast Narrowing Doubts
primitive widening doubt