Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help about wrapper class constructor 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 "Help about wrapper class constructor" Watch "Help about wrapper class constructor" New topic

Help about wrapper class constructor

Peixiao Lin

Joined: Oct 04, 2004
Posts: 28

It seems I can not pass int value to Byte constructor except explicitly casting and using String? Well, if data type matters, why I can pass double value to Float constructor? please help me out.
marc weber

Joined: Aug 31, 2004
Posts: 11343

In general, methods will automatically widen a primitive data type in order to match an expected argument type, but they will not narrow it (because this could result in a loss of precision). This automatic widening is called method-call conversion.

Note that a literal int value can be assigned to a narrower primitive type if it is within range. However, this narrowing is only for assignments -- not method arguments.

The Float constructor is a little unusual (compared to the other numeric wrappers) because it's overloaded to accept a float or a double. In the case of a double, the argument is converted to a float before wrapping.
[ October 17, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: Help about wrapper class constructor
It's not a secret anymore!