File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Wrapper class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper class" Watch "Wrapper class" New topic
Author

Wrapper class

C Law
Greenhorn

Joined: Mar 05, 2006
Posts: 21
Float f = 13.0; // compiler error

Float f = new Float(13.0); //OK

Can anybody tell me why this is so?

Thanks.
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Originally posted by C Law:
Float f = 13.0; // compiler error

Float f = new Float(13.0); //OK

Can anybody tell me why this is so?

Thanks.


Float is wrapper class for float primitive data type. When you create new object you need to use new keyword except for String.
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
And note that if you are using JDK 1.5, the assignment of a primitive to an object (or vice versa) is perfectly valid. The code

won't generate any compilation error. This feature is called as Auto-boxing.


Mani
Quaerendo Invenietis
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Originally posted by Mani Ram:
And note that if you are using JDK 1.5, the assignment of a primitive to an object (or vice versa) is perfectly valid. The code

won't generate any compilation error. This feature is called as Auto-boxing.


Thanks Mani Ram, I forgot to mention this.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Recall that floating-point literals (like 13.0) are interpreted as type double.

Autoboxing will convert a float to a Float or a double to a Double, but not a double to a Float. See JLS: 5.1.7. This is why Float f = 13.0; fails.

However, there is a Float constructor that takes a double as an argument. This is why Float f = new Float(13.0); is okay.


"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
sscce.org
C Law
Greenhorn

Joined: Mar 05, 2006
Posts: 21
Thanks for the reply.

I am using JDK 1.5.

The point is actually not autoboxing.
On my machine,

Float f = 13.0; // compiler error
Float f = 13.0f; //Autoboxing works.

I just can't see why I have to explicity designate a float
to be autoboxed, especially Float's constructor accepts
double.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mani Ram:
And note that if you are using JDK 1.5, the assignment of a primitive to an object (or vice versa) is perfectly valid...

Not in all cases.

Double d = 13.0; //OK
Float f = 13.0f; //OK

But...

Float f2 = 13.0; //error!

incompatible types
found : double
required: java.lang.Float
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Oops...you are right. Just forgot to remember the basics
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper class